#!/usr/bin/env python3
"""ManageSieve client"""

#
# Copyright 2023 and 2024  Odin Kroeger
#
# This file is part of SieveManager.
#
# SieveManager is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# SieveManager is distributed in the hope that it will be useful,
# but WITHOUT ALL WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SieveManager. If not, see <https://www.gnu.org/licenses/>.
#


#
# Modules
#

from __future__ import annotations
from abc import ABC, abstractmethod
from base64 import b64decode, b64encode
from collections import defaultdict
from collections.abc import Iterable
from dataclasses import dataclass
from errno import EEXIST, EISCONN, ENOENT, ENOTCONN
from fcntl import LOCK_SH, LOCK_NB
from getopt import GetoptError, getopt
from inspect import Parameter
from os import O_CREAT, O_EXCL, O_WRONLY, O_TRUNC, SEEK_END, SEEK_SET
from os import path
from signal import SIG_DFL, SIG_IGN, SIGHUP, SIGINT, SIGTERM
from tempfile import SpooledTemporaryFile, TemporaryDirectory
from typing import (Any, Callable, ClassVar, Final, IO, Iterator,
                    Optional, Union, Sequence, TextIO, TypeVar)

import code
import contextlib
import dataclasses
import datetime
import enum
import fcntl
import fnmatch
import getpass
import hashlib
import hmac
import inspect
import io
import ipaddress
import itertools
import locale
import logging
import math
import netrc
import os
import pwd
import random
import re
import readline
import secrets
import select
import shlex
import shutil
import signal
import socket
import subprocess   # nosec B404
import ssl
import string
import stringprep
import sys
import termios
import threading
import types
import unicodedata
import urllib.parse
import urllib.request

try:
    from cryptography import x509
    from cryptography.hazmat.primitives.hashes import SHA1
    from cryptography.hazmat.primitives.serialization import Encoding
    from cryptography.x509 import (AuthorityInformationAccess,
                                   ExtensionNotFound, ocsp)
    from cryptography.x509.oid import AuthorityInformationAccessOID
    from cryptography.x509.ocsp import OCSPResponseStatus, OCSPCertStatus
except ImportError:
    pass

try:
    import dns
    from dns.exception import DNSException
except ImportError:
    pass


#
# Metadata
#

__version__ = '0.7.2'
__author__ = 'Odin Kroeger'
__copyright__ = '2023 and 2024 Odin Kroeger'
__license__ = 'GPLv3+'
__all__ = [
    # SASL
    'AuthMech',
    'AuthState',
    'AuthType',
    'SASLPrep',
    'AbstractSASLAdapter',
    'AbstractAuth',
    'BaseAuth',
    'BasePassAuth',
    'BaseScramAuth',
    'BaseScramPlusAuth',
    'AuthorisationUnsupportedTrait',
    'CramMD5Auth',
    'ExternalAuth',
    'LoginAuth',
    'PlainAuth',
    'ExternalAuth',
    'ScramSHA1Auth',
    'ScramSHA1PlusAuth',
    'ScramSHA224Auth',
    'ScramSHA224PlusAuth',
    'ScramSHA256Auth',
    'ScramSHA256PlusAuth',
    'ScramSHA384Auth',
    'ScramSHA384PlusAuth',
    'ScramSHA512Auth',
    'ScramSHA512PlusAuth',
    'ScramSHA3_512Auth',
    'ScramSHA3_512PlusAuth',

    # ACAP
    'ACAPAtom',
    'ACAPWord',
    'ACAPLine',
    'BaseACAPConn',

    # ManageSieve
    'BackupPol',
    'Capabilities',
    'Response',
    'URL',
    'SieveConn',
    'SieveManager',
    'SieveSASLAdapter',

    # Logging
    'LogIOWrapper',

    # Errors
    'Error',
    'CapabilityError',
    'ConfigError',
    'DataError',
    'OperationError',
    'ProtocolError',
    'SecurityError',
    'UsageError',
    'ClientError',
    'ClientConnectionError',
    'ClientOperationError',
    'DNSError',
    'DNSOperationError',
    'OCSPError',
    'OCSPDataError',
    'OCSPOperationError',
    'SASLError',
    'SASLCapabilityError',
    'SASLProtocolError',
    'SASLSecurityError',
    'SieveError',
    'SieveCapabilityError',
    'SieveConnectionError',
    'SieveOperationError',
    'SieveProtocolError',
    'TLSError',
    'TLSCapabilityError',
    'TLSSecurityError',

    # Constants
    'ENCODING'
]


#
# Globals
#

ABOUT: Final[str] = f"SieveManager {__version__}\nCopyright {__copyright__}"
"""About message."""

DEBUG: bool = False
"""Print stack traces even for expected error types?"""

EDITOR: Final[list[str]] = shlex.split(os.getenv('EDITOR', 'ed'), posix=True)
""":envvar:`EDITOR` or :command:`ed` if :envvar:`EDITOR` is unset."""

ENCODING: Final[str] = locale.getpreferredencoding()
"""Preferred encoding as returned by :func:`locale.getpreferredencoding`."""

HOMEDIR: Final[str] = os.getenv('HOME', pwd.getpwuid(os.getuid()).pw_dir)
"""Home directory."""

PAGER: Final[list[str]] = shlex.split(os.getenv('PAGER', 'more'), posix=True)
""":envvar:`PAGER` or :command:`more` if :envvar:`PAGER` is unset."""

VISUAL: Final[list[str]] = shlex.split(os.getenv('VISUAL', 'vi'), posix=True)
""":envvar:`VISUAL` or :command:`vi` if :envvar:`VISUAL` is unset."""

XDGCONFIGDIR: Final[str] = os.getenv('XDG_CONFIG_HOME', f'{HOMEDIR}/.config')
"""X Desktop group base configuration directory."""


#
# Python version
#

if sys.version_info < (3, 9):
    sys.exit('SieveManager requires Python 3.9 or later')


#
# Types
#

class LogLevel(enum.IntEnum):
    """Logging levels."""

    AUTH = logging.DEBUG // 2
    DEBUG = logging.DEBUG
    INFO = logging.INFO
    WARNING = logging.WARNING
    ERROR = logging.ERROR


class ShellCmd(enum.IntEnum):
    """Shell actions that may overwrite or remove files."""

    NONE = 0
    CP = 1
    GET = 2
    MV = 4
    PUT = 8
    RM = 16
    ALL = 31


T = TypeVar('T')
"""Type variable."""


#
# Authentication
#

AuthMech = type['AbstractAuth']
"""Alias for subclasses of :class:`AbstractAuth`."""


class AuthState(enum.IntEnum):
    """State of the authentication process."""

    PREAUTH = enum.auto()
    """"AUTHENTICATE" has *not* been issued."""

    SENT = enum.auto()
    """Data sent, ready to receive."""

    RECEIVED = enum.auto()
    """Data received, ready to send."""

    DONE = enum.auto()
    """Authentication concluded."""


class AuthType(enum.IntEnum):
    """Authentication type."""

    OTHER = enum.auto()
    PASSWORD = enum.auto()
    EXTERNAL = enum.auto()


class SASLPrep(enum.IntEnum):
    """Controls which strings are prepared for authentication.

    .. seealso::
        :RFC:`4013`
            SASLPrep
        :RFC:`4422` (sec. 4)
            SASL protocol requirements
    """

    NONE = 0
    USERNAMES = 1
    PASSWORDS = 2
    ALL = 3


class AbstractSASLAdapter(ABC):
    """Adapter to send SASL messages over another protocol.

    .. seealso::
        :class:`AbstractAuth`
            Abstract base class for performing SASL authentication.
        :RFC:`4422`
            Simple Authentication and Security Layer (SASL)
    """

    @abstractmethod
    def abort(self):
        """Abort authentication."""

    @abstractmethod
    def begin(self, name: str, data: Optional[bytes] = None):
        """Begin authentication.

        Arguments:
            name: SASL mechanism name.
            data: Optional client-first message.
        """

    @abstractmethod
    def end(self):
        """Conclude authentication.

        Raises:
            OperationError: Authentication failed.
        """

    @abstractmethod
    def send(self, data: bytes):
        """Encode and send an SASL message."""

    @abstractmethod
    def receive(self) -> bytes:
        """Receive and decode an SASL message."""

    sock: Union[socket.SocketType, ssl.SSLSocket]
    """Underlying socket."""


class AbstractAuth(ABC):
    """Abstract base class for SASL mechanisms.

    .. seealso::
        :class:`AbstractSASLAdapter`
            Abstract base class for sending SASL messages.
        https://www.iana.org/assignments/sasl-mechanisms/sasl-mechanisms.xhtml
            Authentication mechanisms registered with IANA_.
    """

    @abstractmethod
    def __init__(self, adapter: AbstractSASLAdapter,
                 authcid: str, authzid: str = '',
                 prepare: SASLPrep = SASLPrep.ALL):
        """Prepare authentication.

        Arguments:
            authcid: Authentication ID (user to login as).
            authzid: Authorisation ID (use whose rights to acquire).
            prepare: Which credentials to prepare.

        Raises:
            ValueError: Bad characters in username.
        """

    @abstractmethod
    def __call__(self) -> Optional[Any]:
        """Perform authentication.

        Returns:
            Data returned by the server, if any.
        """

    name: ClassVar[str]
    """Mechanism name."""

    authcid: str
    """Authentication ID (user to login as)."""

    authzid: str = ''
    """Authorisation ID (user whose rights to acquire)."""


class BaseAuth(AbstractAuth, ABC):
    """Base class for authentication mechanisms.

    .. note::
        :class:`BaseAuth` handles SASL compression transparently.
        Most mechanisms should be implemented by overriding
        :meth:`__init__` and :meth:`exchange` and useing
        :meth:`send` and :meth:`receive`.
    """

    @staticmethod
    # pylint: disable=redefined-outer-name
    def prepare(string: str) -> str:
        """Prepare a string according to the SASLprep profile.

        Raises:
            ValueError: String contains bad characters.

        .. seealso::
            :RFC:`3454`
                Stringprep
            :RFC:`4013`
                SASLPrep
        """
        prep = ''
        normalised = unicodedata.ucd_3_2_0.normalize('NFKC', string)
        for i, char in enumerate(normalised, start=1):
            if stringprep.in_table_b1(char):
                pass
            elif stringprep.in_table_c12(char):
                prep += ' '
            elif stringprep.in_table_c21_c22(char):
                raise ValueError(f'{string}:{i}: control character')
            elif stringprep.in_table_c3(char):
                raise ValueError(f'{string}:{i}: private use character')
            elif stringprep.in_table_c4(char):
                raise ValueError(f'{string}:{i}: non-character code point')
            elif stringprep.in_table_c5(char):
                raise ValueError(f'{string}:{i}: surrogate code point')
            elif stringprep.in_table_c6(char):
                raise ValueError(f'{string}:{i}: not plain text')
            elif stringprep.in_table_c7(char):
                raise ValueError(f'{string}:{i}: not canonical')
            elif stringprep.in_table_c8(char):
                raise ValueError(f'{string}:{i}: changes display/deprecated')
            elif stringprep.in_table_c9(char):
                raise ValueError(f'{string}:{i}: tagging character')
            else:
                prep += char
        return prep

    @classmethod
    def getmechs(cls, sort: bool = True, obsolete: bool = False) \
            -> Iterable[type['BaseAuth']]:
        """Get authentication mechanisms.

        Arguments:
            sort: Sort mechanisms by :attr:`order`?
            obsolete: Return obsolete mechanisms, too?
        """
        mechs: set[type['BaseAuth']] = set()
        for subcls in cls.__subclasses__():
            # pylint: disable=bad-indentation
            if (not subcls.__abstractmethods__
                and (obsolete or not subcls.obsolete)):
                    mechs.add(subcls)
            mechs.update(subcls.getmechs(sort=False, obsolete=obsolete))
        if sort:
            return list(sorted(mechs, key=lambda c: c.order))
        return mechs

    def __init__(self, adapter: AbstractSASLAdapter,
                 authcid: str, authzid: str = '',
                 prepare: SASLPrep = SASLPrep.ALL):
        """Prepare authentication.

        Arguments:
            authcid: Authentication ID (user to login as).
            authzid: Authorisation ID (use whose rights to acquire).
            prepare: Which credentials to :meth:`prepare`.

        Raises:
            ValueError: Bad characters in username.
        """
        prepare &= SASLPrep.USERNAMES   # type: ignore[assignment]
        self.adapter = adapter
        self.authcid = self.prepare(authcid) if prepare else authcid
        self.authzid = self.prepare(authzid) if prepare else authzid

    # pylint: disable=useless-return
    def __call__(self) -> Optional[Capabilities]:
        """Perform authentication.

        .. note::
            Calls :meth:`exchange` and :meth:`end`.
        """
        self.exchange()
        if self.state == AuthState.RECEIVED:
            self.send(b'')
        self.end()
        return None

    def abort(self):
        """Abort authentication."""
        self.adapter.abort()
        self.state = AuthState.DONE

    def begin(self, data: Optional[bytes] = None):
        """Begin authentication.

        Arguments:
            data: Optional client-first message.
        """
        if self.state == AuthState.PREAUTH:
            self.adapter.begin(self.name, data)
            self.state = AuthState.SENT
        else:
            raise SASLProtocolError(f'SASL state {self.state}: unexpected')

    def end(self):
        """Conclude authentication."""
        if self.state == AuthState.SENT:
            self.adapter.end()
        else:
            raise SASLProtocolError(f'SASL state {self.state}: unexpected')

    @abstractmethod
    def exchange(self):
        """Exchange SASL messages."""

    def send(self, data: bytes):
        """Encode and send an SASL message.

        .. note::
            Calls :meth:`begin` if needed.
        """
        if self.state == AuthState.PREAUTH:
            self.begin(data)
        elif self.state == AuthState.RECEIVED:
            self.adapter.send(data)
            self.state = AuthState.SENT
        else:
            raise SASLProtocolError(f'SASL state {self.state}: unexpected')

    def receive(self) -> bytes:
        """Receive and decode an SASL message.

        .. note::
            Calls :meth:`begin` if needed.
        """
        if self.state == AuthState.PREAUTH:
            self.begin()
        if self.state == AuthState.SENT:
            try:
                data = self.adapter.receive()
                self.state = AuthState.RECEIVED
            except SieveOperationError as err:
                if err.response != 'ok':
                    raise
                if not err.matches('sasl'):
                    # pylint: disable=raise-missing-from
                    raise SASLProtocolError('expected data')
                try:
                    word = err.code[1]
                except ValueError as valuerr:
                    raise SASLProtocolError('unexpected data') from valuerr
                if isinstance(word, ACAPAtom) or not isinstance(word, str):
                    # pylint: disable=raise-missing-from
                    raise SASLProtocolError('expected string')
                data = b64decode(word)
                self.state = AuthState.DONE
            return data
        raise SASLProtocolError(f'SASL state {self.state}: unexpected')

    @property
    def sock(self) -> Union[socket.SocketType, ssl.SSLSocket]:
        """Underlying socket."""
        assert self.adapter   # nosec B101
        return self.adapter.sock

    @sock.setter
    def sock(self, sock: Union[socket.SocketType, ssl.SSLSocket]):
        assert self.adapter   # nosec B101
        self.adapter.sock = sock

    adapter: AbstractSASLAdapter
    """Underlying SASL adapter."""

    obsolete: bool = False
    """Is this mechanism obsolete?"""

    order: int = 0
    """Mechanism precedence."""

    state: AuthState = AuthState.PREAUTH
    """Current authentication state."""

    type: ClassVar[AuthType] = AuthType.OTHER
    """Authentication type."""


class BasePassAuth(BaseAuth, ABC):
    """Base class for password-based authentication mechanisms."""

    def __init__(self, connection: AbstractSASLAdapter,
                 authcid: str, password: str, authzid: str = '',
                 prepare: SASLPrep = SASLPrep.ALL):
        """Prepare authentication.

        Arguments:
            authcid: Authentication ID (user to login as).
            authzid: Authorisation ID (use whose rights to acquire).
            password: Password.
            prepare: Which credentials to :meth:`BaseAuth.prepare`.

        Raises:
            ValueError: Bad characters in username or password.
        """
        super().__init__(connection, authcid, authzid, prepare)
        prepare &= SASLPrep.PASSWORDS   # type: ignore[assignment]
        self.password = self.prepare(password) if prepare else password

    password: str
    """Password."""

    type = AuthType.PASSWORD


class BaseScramAuth(BasePassAuth, ABC):
    """Base class for SCRAM authentication mechanisms.

    .. seealso::
        :RFC:`5802`
            Salted Challenge Response Authentication Mechanism (SCRAM).
        :RFC:`7677`
            SCRAM-SHA-256 and SCRAM-SHA-256-PLUS.
        https://datatracker.ietf.org/doc/html/draft-melnikov-scram-bis
            Updated recommendations for implementing SCRAM.
        https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha-512-03
            SCRAM-SHA-512 and SCRAM-SHA-512-PLUS.
        https://datatracker.ietf.org/doc/html/draft-melnikov-scram-sha3-512-03
            SCRAM-SHA3-512 and SCRAM-SHA3-512-PLUS.
        https://csb.stevekerrison.com/post/2022-01-channel-binding
            Discussion of TLS channel binding.
        https://csb.stevekerrison.com/post/2022-05-scram-detail
            Discussion of SCRAM.
    """

    def exchange(self):
        # Compare to
        # * https://github.com/stevekerrison/auth-examples
        # * https://github.com/horazont/aiosasl

        def todict(msg: bytes) -> dict[bytes, bytes]:
            return dict([a.split(b'=', maxsplit=1) for a in msg.split(b',')])

        # Parameters
        authcid = self.authcid.encode('utf8')
        authzid = self.authzid.encode('utf8')
        password = self.password.encode('utf8')
        chan_bind_type = self.cbtype.encode('utf8')
        chan_bind_data = self.cbdata
        c_nonce_len = self.noncelen
        digest = self.digest

        # Send client-first message
        chan_bind_attr = b'p=%s' % chan_bind_type if chan_bind_type else b'n'
        c_first_prefix = chan_bind_attr + b',' + authzid
        c_nonce = b64encode(secrets.token_bytes(c_nonce_len))
        c_first_bare = b'n=%s,r=%s' % (authcid, c_nonce)
        c_first = c_first_prefix + b',' + c_first_bare
        self.send(c_first)

        # Receive server-first message
        try:
            s_first = self.receive()
        except SieveOperationError as err:
            # pylint: disable=bad-exception-cause (???)
            raise SASLCapabilityError(f'{self.name}: {err}') from err
        s_first_dict = todict(s_first)
        iters = int(s_first_dict[b'i'])
        s_nonce = s_first_dict[b'r']
        salt = b64decode(s_first_dict[b's'])

        # Send client-final message
        salted_pwd = hashlib.pbkdf2_hmac(digest, password, salt, iters)
        c_key = hmac.digest(salted_pwd, b'Client Key', digest)
        stored_key = hashlib.new(digest, c_key).digest()
        chan_bind = b64encode(c_first_prefix + b',' + chan_bind_data)
        c_final_prefix = b'c=%s,r=%s' % (chan_bind, s_nonce)
        auth_message = b','.join((c_first_bare, s_first, c_final_prefix))
        c_signature = hmac.digest(stored_key, auth_message, digest)
        c_proof = b64encode(bytes(a ^ b for a, b in zip(c_key, c_signature)))
        c_final = c_final_prefix + b',p=%s' % c_proof
        self.send(c_final)

        # Receive server-final message
        s_final = self.receive()
        s_key = hmac.digest(salted_pwd, b'Server Key', digest)
        s_signature = hmac.digest(s_key, auth_message, digest)
        s_final_dict = todict(s_final)
        if s_signature != b64decode(s_final_dict[b'v']):
            host = self.sock.getpeername()
            raise SASLSecurityError(f'{host}: verification failed')

    @property
    @abstractmethod
    def digest(self) -> str:
        """Digest name as used by :mod:`hashlib` and :mod:`hmac`."""

    cbtype: str = ''
    """TLS channel-binding type."""

    cbdata: bytes = b''
    """TLS channel-binding data."""

    noncelen: int = 18
    """Client nonce length in bytes."""


class BaseScramPlusAuth(BaseScramAuth, ABC):
    """Base class for SCRAM mechanisms with channel binding."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if not isinstance(self.sock, ssl.SSLSocket):
            raise SASLProtocolError('non-TLS channel cannot be bound')
        for cbtype in ('tls-exporter', 'tls-unique', 'tls-server-endpoint'):
            if cbtype in ssl.CHANNEL_BINDING_TYPES:
                if cbdata := self.sock.get_channel_binding(cbtype):
                    self.cbtype = cbtype
                    self.cbdata = cbdata
                    break
        else:
            raise TLSCapabilityError('no supported channel-binding type')


# pylint: disable=no-member
class AuthorisationUnsupportedTrait():
    """Trait for SASL mechanisms that do not support authorisation."""

    def __init__(self, *args, **kwargs):
        """Initialise the SASL mechanism.

        Raises:
            SASLCapabilityError: :attr:`authzid` is set."""
        assert isinstance(self, BaseAuth)   # nosec B101
        super().__init__(*args, **kwargs)
        if self.authzid:
            message = f'{self.name}: no authorisation'
            raise SASLCapabilityError(message)


class CramMD5Auth(AuthorisationUnsupportedTrait, BasePassAuth):
    """CRAM-MD5 authentication.

    .. seealso::
        :RFC:`2195` (sec. 2)
            Definition of CRAM-MD5.
    """

    def exchange(self):
        challenge = self.receive()
        password = self.password.encode('utf8')
        digest = hmac.new(password, challenge, hashlib.md5)
        data = ' '.join((self.authcid, digest.hexdigest()))
        self.send(data.encode('utf8'))

    obsolete = True
    name = 'CRAM-MD5'


class ExternalAuth(BaseAuth):
    """EXTERNAL authentication.

    .. seealso::
        :RFC:`4422` (App. A)
            Definition of the EXTERNAL mechanism.
    """

    def __call__(self):
        """Authenticate."""
        args = (self.authzid.encode('utf8'),) if self.authzid else ()
        self.begin(*args)
        self.receive()
        self.send(b'')
        self.end()

    def exchange(self):
        """No-op."""

    name = 'EXTERNAL'
    type = AuthType.EXTERNAL


class LoginAuth(AuthorisationUnsupportedTrait, BasePassAuth):
    """LOGIN authentication.

    .. seealso::
        https://datatracker.ietf.org/doc/draft-murchison-sasl-login
            Definition of the LOGIN mechanism.
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if {self.password} & {'\r', '\n', '\0'}:
            raise ValueError('password: contains CR, LF, or NUL')

    def exchange(self):
        self.receive()
        self.send(self.authcid.encode('utf8'))
        self.receive()
        self.send(self.password.encode('utf8'))

    obsolete = True
    name = 'LOGIN'


class PlainAuth(BasePassAuth):
    """PLAIN authentication.

    .. seealso::
        :RFC:`4616`
            PLAIN authentication mechanism.
    """

    def exchange(self):
        data = '\0'.join((self.authzid, self.authcid, self.password))
        self.send(data.encode('utf8'))

    name = 'PLAIN'


class ScramSHA1Auth(BaseScramAuth):
    """SCRAM-SHA-1 authentication."""
    digest = 'sha1'
    name = 'SCRAM-SHA-1'
    order = -10


class ScramSHA1PlusAuth(BaseScramPlusAuth, ScramSHA1Auth):
    """SCRAM-SHA-1-PLUS authentication."""
    name = 'SCRAM-SHA-1-PLUS'
    order = -1000


class ScramSHA224Auth(BaseScramAuth):
    """SCRAM-SHA-224 authentication."""
    digest = 'sha224'
    name = 'SCRAM-SHA-224'
    order = -20


class ScramSHA224PlusAuth(BaseScramPlusAuth, ScramSHA224Auth):
    """SCRAM-SHA-224-PLUS authentication."""
    name = 'SCRAM-SHA-224-PLUS'
    order = -2000


class ScramSHA256Auth(BaseScramAuth):
    """SCRAM-SHA-256 authentication."""
    digest = 'sha256'
    name = 'SCRAM-SHA-256'
    order = -30


class ScramSHA256PlusAuth(BaseScramPlusAuth, ScramSHA256Auth):
    """SCRAM-SHA-256-PLUS authentication."""
    name = 'SCRAM-SHA-256-PLUS'
    order = -3000


class ScramSHA384Auth(BaseScramAuth):
    """SCRAM-SHA-384 authentication."""
    digest = 'sha384'
    name = 'SCRAM-SHA-384'
    order = -40


class ScramSHA384PlusAuth(BaseScramPlusAuth, ScramSHA384Auth):
    """SCRAM-SHA-384-PLUS authentication."""
    name = 'SCRAM-SHA-384-PLUS'
    order = -4000


class ScramSHA512Auth(BaseScramAuth):
    """SCRAM-SHA-512 authentication."""
    digest = 'sha512'
    name = 'SCRAM-SHA-512'
    order = -50


class ScramSHA512PlusAuth(BaseScramPlusAuth, ScramSHA512Auth):
    """SCRAM-SHA-512-PLUS authentication."""
    name = 'SCRAM-SHA-512-PLUS'
    order = -5000


# pylint: disable=invalid-name
class ScramSHA3_512Auth(BaseScramAuth):
    """SCRAM-SHA-512 authentication."""
    digest = 'sha3_512'
    name = 'SCRAM-SHA3-512'
    order = -60


# pylint: disable=invalid-name
class ScramSHA3_512PlusAuth(BaseScramPlusAuth, ScramSHA3_512Auth):
    """SCRAM-SHA-512-PLUS authentication."""
    name = 'SCRAM-SHA3-512-PLUS'
    order = -6000


#
# ACAP
#

class ACAPAtom(str):
    """ACAP keyword."""

    data: list['ACAPWord'] = []
    """Data attached to an atom."""


ACAPWord = Union[ACAPAtom, None, int, str, 'ACAPLine']
"""ACAP word."""


ACAPLine = list[ACAPWord]
""":class:`list` of ACAP :class:`ACAPWord`-s."""


class BaseACAPConn(ABC):
    """Application Control Access Protocol (ACAP) parser/serialiser.

    ManageSieve builds on the syntax and data types of ACAP.

    .. seealso::
        :RFC:`2244` (secs. 2.2 and 2.6)
            ACAP commands, responses, and data formats.
        :RFC:`5804` (secs. 1.2 and 4)
            ManageSieve syntax.
    """

    def receiveline(self) -> 'ACAPLine':
        """Receive a line and parse it.

        ==================  =================
        ACAP type           Python type
        ==================  =================
        Atom                :class:`ACAPAtom`
        Literal             :class:`str`
        Nil                 ``None``
        Number              :class:`int`
        Parenthesised List  :class:`list`
        String              :class:`str`
        ==================  =================

        For example:

        >>> acap.sendline(ACAPAtom('listscripts'))
        >>> acap.receiveline()
        ['foo.sieve', 'ACTIVE']
        >>> acap.receiveline()
        ['bar.sieve']
        >>> acap.receiveline()
        ['baz.sieve']
        >>> acap.receiveline()
        ['OK', 'Listscripts completed.']
        """
        assert self.file   # nosec B101
        words: ACAPLine = []
        stack: list[ACAPLine] = [words]
        ptr: ACAPLine = words
        while line := self.file.readline().decode('utf8'):
            size: int = -1
            for token in self._lex(line):
                key = token.lastgroup
                value = token.group(key)    # type: ignore[arg-type]
                if key == 'leftparen':
                    parens: list[ACAPWord] = []
                    stack.append(ptr)
                    ptr.append(parens)
                    ptr = parens
                elif key == 'rightparen':
                    ptr = stack.pop()
                elif key == 'atom':
                    if value.casefold() == 'nil':
                        ptr.append(None)
                    else:
                        ptr.append(ACAPAtom(value))
                elif key == 'number':
                    ptr.append(int(value))
                elif key == 'string':
                    ptr.append(value)
                elif key == 'literal':
                    size = int(value)
                    literal = self.file.read(size).decode('utf8')
                    ptr.append(literal)
            if size == -1:
                break
        return words

    def sendline(self, *objs: Union[IO[Any], 'ACAPWord'], whole: bool = True):
        """Convert objects to ACAP types and send them as a line.

        ==================  ======================================
        Python type         ACAP type
        ==================  ======================================
        :class:`ACAPAtom`   Atom
        :class:`io.IOBase`  Literal
        ``None``            Nil
        :class:`bytes`      Literal or String [#literal]_
        :class:`list`       Parenthesised List
        :class:`int`        Number [#nums]_
        :class:`str`        Literal or String [#literal]_ [#utf8]_
        ==================  ======================================

        For example:

        >>> acap.sendline(ACAPAtom('havespace'), 'script.sieve', 12345)
        >>> acap.receiveline()
        ['OK', 'Putscript would succeed.']

        Pipeline commands:

        >>> conn.isalive(check=True)
        >>> conn.sendline(ACAPAtom('putscript', script, script.name))
        >>> conn.sendline(ACAPAtom('logout'))
        >>> for _ in range(2):
        >>>     conn.collect(check=True)

        Arguments:
            objs: Objects to serialise.
            whole: Conclude output with CRLF?

        Raises:
            ValueError: `int` not in [0, 4,294,967,295].

        .. [#literal] Depending on content.
        .. [#nums] Numbers must be representable as unsigned 32-bit integers.
        .. [#utf8] Strings are encoded in UTF-8 and normalised to form C.
        """
        assert self.file        # nosec B101

        write = self.file.write
        normalize = unicodedata.normalize
        matchstr = self._matchstr

        def encode(s: str) -> bytes:
            return normalize('NFC', s).encode('utf8')

        def addstring(b: bytes):
            write(b'"%s"' % b if matchstr(b) else
                  b'{%d+}\r\n%s' % (len(b), b))

        for i, obj in enumerate(objs):
            if i > 0:
                write(b' ')
            if obj is None:
                write(b'NIL')
            elif isinstance(obj, ACAPAtom):
                write(encode(obj))
            elif isinstance(obj, int):
                if not 0 <= obj < 4_294_967_296:
                    raise ValueError(f'{obj}: not in [0, 4,294,967,295]')
                write(encode(str(obj)))
            elif isinstance(obj, bytes):
                addstring(obj)
            elif isinstance(obj, str):
                addstring(encode(obj))
            elif isinstance(obj, (io.IOBase, SpooledTemporaryFile)):
                write(b'{%d+}\r\n' % file_getsize(obj))
                while block := obj.read(io.DEFAULT_BUFFER_SIZE):
                    write(encode(block) if isinstance(block, str) else block)
            elif isinstance(obj, (Iterator, Sequence)):
                write(b'(')
                self.sendline(*obj, whole=False)
                write(b')')
            else:
                raise TypeError(type(obj).__name__ + ': not an ACAP type')

        if whole:
            write(b'\r\n')
            self.file.flush()

    @property
    @abstractmethod
    def file(self) -> Optional[Union[io.IOBase, 'LogIOWrapper']]:
        """File-like access to the underlying connection."""

    _lex: Callable[..., Iterator[re.Match]] = re.compile('|'.join((
        r'(?P<atom>[a-z][^(){\s\\]*)',
        r'(?P<number>\d+)',
        r'"(?P<string>[^\0\r\n"]*)"',
        r'{(?P<literal>\d+)\+?}',
        r'(?P<leftparen>\()',
        r'(?P<rightparen>\))',
    )), flags=re.IGNORECASE).finditer
    """Split a string into ACAP tokens."""

    # Strings may be 1024 octets long, but I limit the length to 1020 octets
    # to allow for errors (two octets for quotations marks, one for the
    # terminating null byte, one for other off-by-one errors).
    _matchstr: Callable[..., Optional[re.Match]] = re.compile(
        br'[^\0\r\n"]{0,1020}'
    ).fullmatch
    """Check whether some bytes can be represented as ACAP string."""


#
# ManageSieve
#

class BackupPol(enum.IntEnum):
    """Emacs-like backup policies.

    .. seealso::
        *GNU Emacs manual* (chap. `19.3.2 <emacs-backup_>`_)
            Discussion of backups.
    """

    NONE = 0
    """Do *not* make backups."""

    NUMBERED = enum.auto()
    """Back up :file:`file` to :file:`file.~{n}~`.

    *n* starts with 1 and increments with each backup.
    """

    SIMPLE = enum.auto()
    """Back up :file:`file` to :file:`file{~}`."""

    def getfname(self, fname: str,
                 getfiles: Callable[[], Iterable[str]] = lambda: ()) \
                    -> Optional[str]:

        """Derive an Emacs-like filename for a backup.

        Arguments:
            fname: Name of the file to backup.
            getfiles: Function that returns a list of existing files.

        .. seealso::
            Emacs manual (chap. `19.3.2 <emacs-backup_>`_)
                Discussion of backups
        """
        if self == BackupPol.NONE:
            return None
        if self == BackupPol.SIMPLE:
            return fname + '~'
        if self == BackupPol.NUMBERED:
            regex = re.compile(re.escape(fname) + r'\.~(\d+)~')
            last = 0
            for file in getfiles():
                if match := regex.fullmatch(file):
                    if (num := int(match.group(1))) > last:
                        last = num
            num = last + 1
            return fname + f'.~{num}~'
        raise ValueError(f'{self}: no such backup policy')


@dataclass
class Capabilities():
    """Server capabilities."""

    @classmethod
    def fromlines(cls, lines: Iterable[ACAPLine],
                  strict: bool = False) -> 'Capabilities':
        """Create a :class:`Capabilities` object from lines."""
        obj = cls()
        for words in lines:
            try:
                key = words[0].casefold()   # type: ignore[union-attr]
            except IndexError as err:
                raise SieveProtocolError('expected word') from err
            except AttributeError as err:
                raise SieveProtocolError('expected string') from err
            try:
                value, = words[1:]
            except ValueError:
                value = ''
            if not isinstance(value, str):
                raise SieveProtocolError('expected string')
            if key in ('implementation', 'language', 'owner', 'version'):
                setattr(obj, key, value)
            elif key in ('notify', 'sasl', 'sieve'):
                setattr(obj, key, value.casefold().split())
            elif key in ('starttls', 'unauthenticate'):
                setattr(obj, key, True)
            elif key == 'maxredirects':
                setattr(obj, key, int(value))
            elif strict:
                raise ValueError(f'{key}: unknown capability')
            else:
                obj.notunderstood[key] = value
        return obj

    implementation: Optional[str] = None
    """Server application."""

    sieve: tuple[str, ...] = ()
    """Supported Sieve modules."""

    language: Optional[str] = None
    """Language."""

    maxredirects: Optional[int] = None
    """Maximum number of redirect operations permitted in a script."""

    notify: tuple[str, ...] = ()
    """URI schema parts for supported notification methods."""

    owner: str = ''
    """Canonical name of the user authorised as."""

    sasl: tuple[str, ...] = ()
    """Supported authentication methods."""

    starttls: bool = False
    """Is "STARTTLS" available?"""

    unauthenticate: bool = False
    """Is "UNAUTHENTICATE" available?"""

    version: Optional[str] = None
    """ManageSieve protocol version."""

    notunderstood: dict = dataclasses.field(default_factory=dict)
    """Capabilities not understood by SieveManager."""


@dataclass(frozen=True)
class Response():
    """Server response to a command.

    .. seealso::
        :RFC:`5804` (secs. 1.2, 1.3, 4, 6.4, and passim)
            ManageSieve responses
    """

    @classmethod
    def fromline(cls, line: ACAPLine) -> 'Response':
        """Create a :class:`Response` object from an :class:`ACAPLine`."""
        # pylint: disable=redefined-outer-name
        code = cls.code
        message = cls.message
        for word in line:
            if word is None:
                raise SieveProtocolError('expected atom, list, or string')
            if isinstance(word, ACAPAtom):
                response = word.casefold()
            elif isinstance(word, list):
                code = tuple(word)
            elif isinstance(word, str):
                message = word
            else:
                raise SieveProtocolError('expected atom, list, or string')
        return cls(response=response, code=code, message=message)

    def __str__(self) -> str:
        """:attr:`self.message` or a stub message."""
        return self.message if self.message else f'server says {self.response}'

    def matches(self, *categories: str) -> bool:
        """Check if :attr:`code[0]` matches one of the given `categories`.

        For example:

        >>> with open('script.sieve') as script:
        >>>     try:
        >>>         mgr.putscript(script, script.name)
        >>>     except SieveOperationError as err:
        >>>         if err.matches('quota'):
        >>>             print('over quota')

        Print more informative messages:

        >>> with open('script.sieve') as script:
        >>>     try:
        >>>         mgr.putscript(script, script.name)
        >>>     except SieveOperationError as err:
        >>>         if err.matches('quota/maxscripts'):
        >>>             print('too many scripts')
        >>>         elif err.matches('quota/maxsize'):
        >>>             print(f'{script.name} is too large')
        >>>         elif err.matches('quota'):
        >>>             print('over quota')
        """
        try:
            rescode = self.code[0]
        except IndexError:
            return False
        assert isinstance(rescode, str)     # nosec B101
        for cat in categories:
            pattern = re.escape(cat) + r'(/|$)'
            if re.match(pattern, rescode, flags=re.IGNORECASE):
                return True
        return False

    def toerror(self) -> 'Error':
        """Convert `Response` into an error."""
        cls: type[SieveError]
        if self.response == 'bye':
            cls = SieveConnectionError
        else:
            cls = SieveOperationError
        return cls(self.response, self.code, self.message)

    response: str
    """``'ok'``, ``'no'``, or ``'bye'``.

    =========  ===========================
    Response   Meaning
    =========  ===========================
    ``'ok'``   Success
    ``'no'``   Failure
    ``'bye'``  Connection closed by server
    =========  ===========================
    """

    code: tuple[ACAPWord, ...] = ()
    """Response code.

    ManageSieve response codes are lists of categories, separated by
    slashes ("/"), where each category is the super-category of the
    next (e.g., "quota/maxsize").

    Some response codes carry data (e.g., ``TAG "SYNC-123"``).

    See :RFC:`5804` (sec. 1.3) for a list of response codes.

    .. warning::
        Servers need *not* return response codes.
    """

    message: Optional[str] = None
    """Human-readable message.

    .. warning::
        Servers need *not* return a message.

    .. note::
        Prefer :meth:`Response.__str__`.
    """


@dataclass(frozen=True)
class SRV():
    """DNS `SRV record`_."""

    priority: int
    weight: int
    host: str
    port: int


@dataclass(frozen=True)
class URL():
    """Sieve URL.

    .. seealso::
        :RFC:`5804` (sec. 3)
            Sieve URL Scheme
    """

    @classmethod
    def fromstr(cls, url: str):
        """Create a :class:`URL` object from a URL string.

        Raises:
            ValueError: Not a valid Sieve URL.
        """
        if not re.match(r'([a-z][a-z0-9\+\-\.]*:)?//', url):
            url = 'sieve://' + url
        parts = urllib.parse.urlsplit(url)
        if parts.query or parts.fragment:
            raise ValueError(f'{url}: not a Sieve URL')
        if not parts.hostname:
            raise ValueError(f'{url}: no host')
        try:
            owner, scriptname = parts.path.split('/', maxsplit=2)[1:]
        except ValueError:
            owner, scriptname = parts.path[1:], None
        return cls(
            scheme=parts.scheme,
            username=parts.username,
            password=parts.password,
            hostname=parts.hostname,
            port=parts.port,
            owner=owner,
            scriptname=scriptname
        )

    def __str__(self):
        """Convert into a URL string."""
        url = ''
        if self.scheme:
            url += f'{self.scheme}://'
        else:
            url += 'sieve://'
        if self.username:
            url += self.username
            if self.password is not None:
                url += f':{self.password}'
            url += '@'
        if self.hostname:
            url += self.hostname
        else:
            url += 'localhost'
        if self.port:
            url += f':{self.port}'
        if self.owner:
            url += f'/{self.owner}'
        if self.scriptname:
            url += f'/{self.scriptname}'
        return url

    hostname: str
    scheme: Optional[str] = None
    username: Optional[str] = None
    password: Optional[str] = None
    port: Optional[int] = None
    owner: Optional[str] = None
    scriptname: Optional[str] = None


class SieveSASLAdapter(AbstractSASLAdapter):
    """Adapter to send SASL messages over a :class:`SieveConn`."""

    def __init__(self, connection: 'SieveConn'):
        """Initialise the adapter."""
        self.conn = connection

    def abort(self):
        self.send(b'*')
        self.end()

    def begin(self, name: str, data: Optional[bytes] = None):
        assert self.conn  # nosec B101
        args: list[Any] = [ACAPAtom('AUTHENTICATE'), name.upper()]
        if data is not None:
            args.append(b64encode(data))
        self.conn.sendline(*args)   # type: ignore[arg-type]

    def end(self):
        assert self.conn  # nosec B101
        res = Response.fromline(self.conn.receiveline())
        if res.response != 'ok':
            raise res.toerror()

    def send(self, data: bytes):
        assert self.conn  # nosec B101
        conn = self.conn
        conn.sendline(b64encode(data))  # type: ignore[arg-type]

    def receive(self) -> bytes:
        assert self.conn  # nosec B101
        line = self.conn.receiveline()
        if isinstance(line[0], ACAPAtom):
            res = Response.fromline(line)
            raise res.toerror()
        try:
            word, = line
        except ValueError as err:
            raise SieveProtocolError('unexpected data') from err
        if isinstance(word, ACAPAtom) or not isinstance(word, str):
            raise SieveProtocolError('expected string')
        return b64decode(word)

    @property
    def sock(self) -> Union[socket.SocketType, ssl.SSLSocket]:
        """Underlying socket."""
        assert self.conn        # nosec B101
        assert self.conn.sock   # nosec B101
        return self.conn.sock

    @sock.setter
    def sock(self, sock: Union[socket.SocketType, ssl.SSLSocket]):
        assert self.conn        # nosec B101
        self.conn.sock = sock

    conn: Optional[SieveConn] = None
    """Underlying connection."""


class SieveConn(BaseACAPConn):
    """Connection to a ManageSieve server.

    For example:

    >>> conn = SieveConn('imap.foo.example', 'user', 'password')
    >>> with open('script.sieve', 'br') as file:
    >>>     res = conn.execute('putscript', file.name, file)
    >>>     if res.response != 'ok':
    >>>         raise res.toerror()
    >>> res = conn.execute('logout')
    >>> if res.response != 'ok':
    >>>     raise res.toerror()

    .. note::
        :class:`SieveConnect` provides a low-level interface.
        You likely want to use :class:`SieveManager` instead.

    .. warning::
        :class:`SieveConn` is not thread-safe.

    .. seealso::
        :RFC:`2244`
            Application Configuration Access Protocol
        :RFC:`2782`
            DNS `SRV records`_
        :RFC:`5804`
            ManageSieve
    """

    def __init__(self, *args, **kwargs):
        """:meth:`Open <open>` a connection to a ManageSieve server.

        Arguments:
            args: Positional arguments for :meth:`open`.
            kwargs: Keyword arguments for :meth:`open`.
        """
        self.lock = threading.Lock()
        self.open(*args, **kwargs)

    def __del__(self):
        """Shut the connection down."""
        try:
            self.shutdown()
        except OSError:
            pass

    def authenticate(self, login: str, *args,   # noqa: C901
                     owner: str = '', sasl: Iterable[AuthMech] = (),
                     logauth: bool = False, **kwargs):
        """Authenticate as `login`.

        `args` and `kwargs` are passed to the constructors
        of the SASL mechanisms given in `sasl`.

        For example, password-based mechanisms require a password:

        >>> conn.authenticate('user', 'password')

        By contrast, the "EXTERNAL" mechanism requires no credentials:

        >>> conn.authenticate('user', sasl=[ExternalAuth])

        By default, authentication is attempted with password-based mechanisms.

        Arguments:
            login: User to login as (authentication ID).
            owner: User whose scripts to manage (authorisation ID).
            sasl: SASL mechanisms (default: non-obsolete, password-based).
            logauth: Log the authentication exchange?
            args: Positional arguments for SASL mechanism constructors.
            kwargs: Keyword arguments for SASL mechanism constructors.

        Raises:
            SASLCapabilityError: Authentication mechanisms exhausted.
            SieveOperationError: Authentication failed.
            ValueError: Bad characters in credentials.
        """
        kwargs['authzid'] = owner
        logger = self.logger
        self.auth = args
        self.kwauth = kwargs
        self.sasl = sasl if sasl else BasePassAuth.getmechs()
        self.logauth = logauth
        while True:
            # pylint: disable=consider-using-with
            if not self.lock.acquire(blocking=False):
                raise ClientOperationError('other operation ongoing')
            if isinstance(self.file, LogIOWrapper):
                self.file.log = logauth
            try:
                for mech in self.sasl:
                    assert self.capabilities    # nosec B101
                    if mech.name.casefold() in self.capabilities.sasl:
                        conn = SieveSASLAdapter(self)
                        try:
                            auth = mech(conn, login, *args, **kwargs)
                            if caps := auth():
                                self.capabilities = caps
                        except SASLCapabilityError as err:
                            logger.error(err)
                            continue
                        except SieveOperationError as err:
                            # TRANSITION-NEEDED need not be treated specially.
                            if err.matches('auth-too-weak', 'encrypt-needed',
                                           'transition-needed'):
                                logger.debug(err)
                                continue
                            raise
                        if login := auth.authcid:
                            self.login = login
                            logger.info('authenticated as %s', login)
                        if owner := auth.authzid:
                            self.owner = owner
                            logger.info('authorised as %s', owner)
                        return
                raise SASLCapabilityError('SASL mechanisms exhausted')
            except SieveConnectionError as err:
                if err.matches('referral'):
                    try:
                        url = err.code[1]
                    except ValueError as exc:
                        raise SieveProtocolError('unexpected data') from exc
                    if isinstance(url, ACAPAtom) or not isinstance(url, str):
                        # pylint: disable=raise-missing-from
                        raise SieveProtocolError('expected string')
                    self._follow(url)
                    continue
                raise
            finally:
                if isinstance(self.file, LogIOWrapper):
                    self.file.log = True
                self.lock.release()
            # NOTREACHED

    def close(self):
        """Close the client-side of the connection.

        .. warning::
            Should only be called if the server has closed the connection.
        """
        if self.file is not None:
            self.file.close()
            self.file = None
        if self.poll is not None:
            assert self.sock     # nosec B101
            self.poll.unregister(self.sock)
            self.poll = None
        if self.sock is not None:
            self.sock.close()
            self.sock = None
        self.capabilities = None
        self.host = None
        self.port = None
        self.login = ''
        self.owner = ''
        self.auth = ()
        self.kwauth = {}

    def collect(self, check: bool = False) \
            -> tuple['Response', list[ACAPLine]]:
        """Collect the server's response to the last transmission.

        For example:

        >>> conn.sendline(ACAPAtom('listscripts'))
        >>> conn.collect()
        (Response(response='ok', code=(), data=(), message=None),
         [['foo.sieve', 'ACTIVE'], ['bar.sieve'], ['baz.sieve']])

        Arguments:
            check: Raise an error if the response is not "OK"?

        Raises:
            SieveConnectionError: Server said "BYE". [#collect-check]_
            SieveOperationError: Server said "NO". [#collect-check]_

        .. [#collect-check] Only raised if `check` is `True`.
        """
        lines: list[ACAPLine] = []
        while True:
            line = self.receiveline()
            if line and isinstance(line[0], ACAPAtom):
                res = Response.fromline(line)
                if check and res.response != 'ok':
                    raise res.toerror()
                self.warning = res.message if res.matches('warnings') else None
                return res, lines
            lines.append(line)
        # NOTREACHED

    def execute(self, command: str, *args: Union[IO, ACAPWord]) \
            -> tuple['Response', list[ACAPLine]]:
        """Execute `command` and return the server's response.

        For example:

            >>> conn.execute('listscripts')
            (Response(response='ok', code=(), data=(), message=None),
             [['foo.sieve', 'ACTIVE'], ['bar.sieve'], ['baz.sieve']])

        Raises:
            SieveConnectionError: Server said "BYE".
            SieveOperationError: Server said "NO".

        .. note::
            Referrals are followed automatically.
        """
        assert command      # nosec B101
        while True:
            # pylint: disable=consider-using-with
            if not self.lock.acquire(blocking=False):
                raise ClientOperationError('other operation ongoing')
            try:
                self.isalive(check=True)
                self.sendline(ACAPAtom(command.upper()), *args)
                res, data = self.collect()
            finally:
                self.lock.release()
            if res.response == 'ok':
                return res, data
            if res.response == 'no':
                raise res.toerror()
            if res.response == 'bye':
                if not res.matches('referral'):
                    raise res.toerror()
                try:
                    url = res.code[1]
                except ValueError as exc:
                    raise SieveProtocolError('unexpected data') from exc
                if isinstance(url, ACAPAtom) or not isinstance(url, str):
                    # pylint: disable=raise-missing-from
                    raise SieveProtocolError('expected string')
                self._follow(url)
            else:
                raise SieveProtocolError(f'unexpected response: {res}')
        # NOTREACHED

    def getstate(self) -> tuple[list, dict[str, Any]]:
        """Get arguments that would re-:meth:`open` the current connection.

        For example:

        >>> conn.geturl()
        sieve://user@imap.foo.example
        >>> args, kwargs = conn.getstate()
        >>> conn.shutdown()
        >>> conn.geturl()
        >>> conn.open(*args, **kwargs)
        >>> conn.geturl()
        sieve://user@imap.foo.example
        """
        args = []
        kwargs = {}
        signature = inspect.signature(self.open)
        for name, param in list(signature.parameters.items()):
            value = getattr(self, name)
            if param.kind in (Parameter.POSITIONAL_ONLY,
                              Parameter.POSITIONAL_OR_KEYWORD):
                args.append(value)
            elif param.kind == Parameter.VAR_POSITIONAL:
                args.extend(value)
            elif param.kind == Parameter.KEYWORD_ONLY:
                kwargs[name] = value
            elif param.kind == Parameter.VAR_KEYWORD:
                kwargs.update(value)
        return (args, kwargs)

    def geturl(self) -> Optional['URL']:
        """URL of the current connection.

        For example:

        >>> conn = SieveConn('imap.foo.example', 'user', 'password')
        >>> conn.geturl()
        'sieve://user@imap.foo.example'

        .. note::
            Only changes to the connection state effected by :meth:`open`,
            :meth:`close`, :meth:`shutdown`, and referrals are tracked.
        """
        if self.host:
            return URL(hostname=self.host,
                       port=self.port if self.port != 4190 else None,
                       username=self.login,
                       owner=self.owner)
        return None

    def isalive(self, check: bool = False) -> bool:
        """Check whether :attr:`sock` is alive.

        Arguments:
            check: Raise an error if :attr:`sock` is dead?

        Raises:
            ClientConnectionError: :attr:`sock` is dead. [#isalive-check]_

        .. [#isalive-check] Only raised if `check` is `True`.
        """
        assert self.poll   # nosec B101
        (_, events), = self.poll.poll()
        try:
            if events & select.POLLERR:
                raise ClientConnectionError(ENOTCONN, 'socket error')
            if events & select.POLLHUP:
                raise ClientConnectionError(ENOTCONN, 'server hung up')
            if events & select.POLLNVAL:
                raise ClientConnectionError(ENOTCONN, os.strerror(ENOTCONN))
        except ClientConnectionError:
            if check:
                raise
        return True

    # pylint: disable=redefined-outer-name,too-many-arguments
    def open(self, host: str, login: str, *auth,
             port: int = 4190, source: tuple[str, int] = ('', 0),
             timeout: Optional[float] = socket.getdefaulttimeout(),
             tls: bool = True, ocsp: bool = True, owner: str = '',
             sasl: Iterable[AuthMech] = (), logauth: bool = False,
             **kwauth):
        """Connect to `host` and login as `login`.

        Connect and login:

        >>> conn.open('imap.foo.example', 'user', 'password')

        Defer login:

        >>> conn.open('imap.foo.example', '')
        >>> conn.authenticate('user', 'password')

        Arguments:
            host: Server address.
            login: User to login as (authentication ID).
                Set to the empty string to defer login.
            auth: Positional arguments for :meth:`authenticate`.
            port: Server port.
            source: Source address and port.
            timeout: Timeout in seconds.
            tls: Secure the connection?
            ocsp: Check whether the server certificate was revoked?
            owner: User whose scripts to manage (authorisation ID).
            sasl: See :meth:`authenticate`.
            logauth: See :meth:`authenticate`.
            kwauth: Keywords arguments for :meth:`authenticate`.

        Raises:
            SASLCapabilityError: Authentication mechanisms exhausted.
            SieveCapabilityError: "STARTTLS" not supported.
            SieveOperationError: Authentication failed.
            ValueError: Bad characters in credentials.
        """
        # pylint: disable=consider-using-with
        if not self.lock.acquire(blocking=False):
            raise ClientOperationError('other operation ongoing')
        try:
            if self.sock or self.file:
                raise ClientConnectionError(EISCONN, os.strerror(EISCONN))
            try:
                if 'dns.resolver' not in sys.modules:
                    raise DNSOperationError('dnspython unavailable')
                if ipaddr_is(host):
                    raise DNSOperationError('{host}: not a hostname')
                # This is the algorithm specified by RFC 2782, NOT the one
                # specified by RFC 5804, sec. 1.8, which is wrong.
                try:
                    for rec in srv_resolve(f'_sieve._tcp.{host}.'):
                        try:
                            self._connect(rec.host, rec.port, source, timeout)
                        except OSError:
                            continue
                        break
                except DNSException as err:
                    # DNSException could raise a NameError below.
                    raise DNSOperationError(f'{host}: not found') from err
            except DNSOperationError:
                self._connect(host, port, source, timeout)
            _, lines = self.collect(check=True)
            self.capabilities = Capabilities.fromlines(lines)
            self.source = source
            self.logger.info('connected to %s:%d', host, port)
        finally:
            self.lock.release()
        if tls:
            self.starttls(ocsp=ocsp)
        if login:
            self.authenticate(login, *auth, owner=owner, sasl=sasl,
                              logauth=logauth, **kwauth)

    def shutdown(self):
        """Shut the connection down.

        .. note::
            Should only be used if logging out would be unsafe.
        """
        if self.sock is not None:
            self.sock.shutdown(socket.SHUT_RDWR)
            self.logger.info('shut connection down')
        self.close()

    # pylint: disable=redefined-outer-name
    def starttls(self, ocsp: bool = True):
        """Start TLS encryption.

        Arguments:
            ocsp: Check whether the server certificate was revoked?

        Raises:
            SieveCapabilityError: "STARTTLS" not supported.

        .. note::
            Called by :meth:`open` unless `tls` is `False`.
        """
        assert self.sock            # nosec B101
        assert self.capabilities    # nosec B101
        if not self.capabilities.starttls:
            raise SieveCapabilityError('STARTTLS: not supported')
        self.execute('STARTTLS')
        # pylint: disable=consider-using-with
        if not self.lock.acquire(blocking=False):
            raise ClientOperationError('other operation ongoing')
        try:
            self.sock = self.sslcontext.wrap_socket(
                self.sock,  # type: ignore[arg-type]
                server_hostname=self.host
            )
            file = self.sock.makefile('rwb')
            self.file = LogIOWrapper.wrapfile(file, logger=self.logger)
            _, lines = self.collect(check=True)
            self.capabilities = Capabilities.fromlines(lines)
            self.ocsp = ocsp
            proto = self.sock.version()
            cipher = self.sock.cipher()
            if proto and cipher:
                self.logger.info('started %s using %s', proto, cipher[0])
            if ocsp:
                if 'cryptography' in sys.modules:
                    der = self.sock.getpeercert(binary_form=True)
                    cert = x509.load_der_x509_certificate(der)
                    if cert_revoked(cert):
                        host = self.host
                        raise TLSSecurityError(f'{host}: certificate revoked')
                else:
                    self.logger.warning('module "cryptography": not found')
        finally:
            self.lock.release()

    def withreopen(self, fn: Callable[..., T], *args, **kwargs) -> T:
        """Call `fn` and reconnect and retry if the connection is closed.

        Arguments:
            args: Positional arguments for `fn`.
            kwargs: Keyword arguments for `fn`.

        Returns:
            The return value of `fn`.
        """
        oargs, okwargs = self.getstate()
        for i in itertools.count():
            try:
                retval = fn(*args, **kwargs)
                break
            except (ConnectionError, OSError) as err:
                print(i, err)
                if i > 0:
                    raise
            self.close()
            self.open(*oargs, **okwargs)
        return retval

    @property
    def timeout(self) -> Optional[float]:
        """Connection timeout in seconds.

        Set timeout to 500 ms:

        >>> conn.timeout = 0.5

        .. note::
            The timeout can only be set while a connection is open.
        """
        if self.sock:
            return self.sock.gettimeout()
        return None

    @timeout.setter
    def timeout(self, secs: Optional[float]):
        if self.sock:
            self.sock.settimeout(secs)

    @property
    def tls(self) -> Optional[str]:     # type: ignore[override]
        """TLS version."""
        if isinstance(self.sock, ssl.SSLSocket):
            return self.sock.version()
        return None

    auth: tuple = ()
    """Positional arguments for SASL mechanism constructors."""

    capabilities: Optional[Capabilities] = None
    """Server capabilities."""

    file: Optional[Union[io.IOBase, 'LogIOWrapper']] = None
    """File-like access to :attr:`sock`."""

    host: Optional[str] = None
    """Remote address."""

    kwauth: dict[str, Any] = {}
    """Keyword arguments for SASL mechanism constructors."""

    lock: threading.Lock
    """Operation lock."""

    logauth: bool
    """Log the authentication exchange?"""

    logger: logging.Logger = logging.getLogger(__name__)
    """Logger to use.

    :class:`SieveConn` logs messages of the following priorities:

    ========================  =====================================
    Priority                  Used for
    ========================  =====================================
    :const:`logging.ERROR`    Non-fatal errors
    :const:`logging.WARNING`  Security warnings
    :const:`logging.INFO`     State changes
    :const:`logging.DEBUG`    Data sent to/received from the server
    ========================  =====================================

    Suppress logging:

    >>> from logging import getLogger
    >>> getLogger('sievemgr').setLevel(logging.CRITICAL)

    Use an individual logger:

    >>> from logging import getLogger
    >>> conn.logger = getLogger('foo').addHandler(logging.NullHandler())

    Print data send to/received from the server to standard error:

    >>> from logging import getLogger
    >>> getLogger('sievemgr').setLevel(logging.DEBUG)
    >>> conn.execute('listscripts')
    C: LISTSCRIPTS
    S: "foo.sieve" ACTIVE
    S: "bar.sieve"
    S: "baz.sieve"
    S: OK "Listscripts completed"
    (Response(response='ok', code=(), data=(), message=None),
     [('foo.sieve', True), ('bar.sieve', False), ('baz.sieve', False)])
    """

    login: str = ''
    """Login name (authentication ID)."""

    ocsp: bool
    """Check whether the server certificate was revoked?"""

    owner: str = ''
    """User whose scripts are managed (authorisation ID)."""

    poll: Optional[select.poll] = None
    """Polling object for :attr:`sock`."""

    port: Optional[int] = None
    """Remote port."""

    sasl: Iterable[AuthMech] = ()
    """SASL mechanisms."""

    sock: Optional[socket.SocketType] = None
    """Underlying socket."""

    source: tuple[str, int] = ('', 0)
    """Source address and port."""

    sslcontext: ssl.SSLContext = ssl.create_default_context()
    """Settings for negotiating Transport Layer Security (TLS).

    Disable workarounds for broken X.509 certificates:

    >>> SieveConn.sslcontext.verify_flags |= ssl.VERIFY_X509_STRICT

    Load client certificate/key pair:

    >>> SieveConn.sslcontext.load_cert_chain(cert='cert.pem')

    Use a custom certificate authority:

    >>> SieveConn.sslcontext.load_verify_locations(cafile='ca.pem')
    """

    warning: Optional[str] = None
    """Warning issued in response to last "CHECKSCRIPT" or "PUTSCRIPT".

    For example:

    >>> with open('script.sieve', 'br') as file:
    >>>     conn.execute('putscript', file, file.name)
    (Response(response='ok', code=('warnings,'),
     data=('line 7: may need to be frobnicated',),
     message=None), [])
    >>> conn.warning
    'line 7: may need to be frobnicated'

    .. note::
        Only set by :meth:`collect`, :meth:`execute`,
        and :class:`SieveManager` methods.

    .. seealso::
        :RFC:`5804` (sec. 1.3)
            ManageSieve "WARNINGS" response code.
    """

    def _connect(self, host: str, port: int = 4190,
                 source: tuple[str, int] = ('', 0),
                 timeout: Optional[float] = socket.getdefaulttimeout()):
        """Connect to `host` at `port`."""
        self.sock = socket.create_connection((host, port), timeout=timeout,
                                             source_address=source)
        file = self.sock.makefile('rwb')
        self.file = LogIOWrapper.wrapfile(file, logger=self.logger)
        self.poll = select.poll()
        self.poll.register(self.sock)
        self.host = host
        self.port = port

    def _follow(self, url: str):
        """Close the current connection and :meth:`open` `url` instead."""
        try:
            ref = URL.fromstr(url)
        except ValueError as err:
            raise SieveProtocolError(err) from err
        self.logger.info('referred to %s', url)
        args, kwargs = self.getstate()
        del args[0]
        del kwargs['port']
        host = ref.hostname
        port = 4190 if ref.port is None else ref.port
        self.close()
        self.open(host, *args, port=port, **kwargs)


class SieveManager(SieveConn, contextlib.AbstractContextManager):
    """High-level interface to a ManageSieve connection.

    For example:

    >>> with SieveManager('imap.foo.example', 'user', 'password') as mgr:
    >>>     with open('sieve.script', 'br') as script:
    >>>         mgr.putscript(script, script.name)
    >>>     mgr.setactive('sieve.script')

    .. note::
        :class:`SieveManager` should *always* be used as a context manager.

    .. warning::
        :class:`SieveManager` is not thread-safe.
    """

    def __init__(self, *args, backup: BackupPol = BackupPol.NONE,
                 memory: int = 524_288, **kwargs):
        """:meth:`Open <open>` a connection to a ManageSieve server.

        Arguments:
            backup: Default backup policy.
            memory: Maximum memory to allocate for temporary files in bytes.
                If files gets larger, they are written to the disk.
            args: Positional arguments for :meth:`SieveConn.open`.
            kwargs: Keyword arguments for :meth:`SieveConn.open`.
        """
        super().__init__(*args, **kwargs)
        self.backup: BackupPol = backup
        self.memory: int = memory

    # pylint: disable=redefined-outer-name
    def __exit__(self, exc_type, exc_value, traceback):
        """Exit the context and close the connection appropriately."""
        if isinstance(exc_value, (ConnectionError, DNSError, TimeoutError)):
            self.close()
        elif isinstance(exc_value, ProtocolError):
            self.shutdown()
        else:
            try:
                self.logout()
            except ClientOperationError:
                try:
                    self.shutdown()
                except OSError:
                    self.close()

    def backupscript(self, script: str, policy: BackupPol = BackupPol.SIMPLE):
        """Make a backup of `script`.

        Arguments:
            script: Script name.
            policy: Backup policy.
        """
        def getscripts() -> tuple[str, ...]:
            return tuple((s for s, _ in self.listscripts()))
        if backup := BackupPol.getfname(policy, script, getscripts):
            self.copyscript(script, backup, backup=BackupPol.NONE)

    def checkscript(self, script: Union[str, IO]):
        """Check whether `script` is semantically valid.

        Arguments:
            script: Script (*not* script name).

        Raises:
            SieveCapabilityError: "CHECKSCRIPT" not supported.
            SieveOperationError: `script` is *not* semantically valid.

        .. important::
            Sieve scripts must be encoded in UTF-8.
        """
        assert self.capabilities    # nosec B101
        if not self.capabilities.version:
            raise SieveCapabilityError('CHECKSCRIPT: not supported')
        self.execute('CHECKSCRIPT', script)

    def copyscript(self, source: str, target: str,
                   backup: Optional[BackupPol] = None):
        """Download `source` and re-upload it as `target`.

        Arguments:
            source: Source name.
            target: Target name.
            backup: Backup policy (default: :attr:`backup`).
        """
        with SpooledTemporaryFile(max_size=self.memory, mode='bw+') as temp:
            temp.write(self.getscript(source).encode('utf8'))
            temp.seek(0)
            self.putscript(temp, target, backup=backup)

    def deletescript(self, script: str):
        """Delete `script`."""
        self._scripts = None
        self.validname(script, check=True)
        self.execute('DELETESCRIPT', script)
        self.logger.info('removed %s', script)

    def editscripts(self, command: list[str], scripts: list[str],   # noqa C901
                    *args, check: bool = True, create: bool = True,
                    errh: Optional[Callable[[Exception], bool]] = None,
                    **kwargs) -> subprocess.CompletedProcess:
        """Download `scripts`, edit them with `command`, and re-upload them.

        Script filenames are appended to the `command`.

        If the server disconnects while the `command` is running,
        the connection is re-established automatically.

        Only scripts that were modified are re-uploaded.

        If re-upload fails and `errh` is given, then the error raised by
        :meth:`putscript` is passed on to `errh`. If `errh` returns `True`,
        `command` is re-invoked for the offending script; otherwise, the
        script is discarded. Either way, the error is suppressed.

        For example:

        >>> mgr.editscripts(['vi'], ['foo.sieve'])

        >>> cp = mgr.editscripts(['cmp'], ['a.sieve', 'b.sieve'], check=False)
        >>> if cp.returncode != 0:
        >>>     print('a.sieve and b.sieve differ')

        Arguments:
            command: Command to run.
            scripts: Scripts to edit.
            check: See :func:`subprocess.run`.
            create: Create scripts that do not exist?
            errh: Error handler.
            args: Positional arguments for :func:`subprocess.run`.
            kwargs: Keywords arguments for :func:`subprocess.run`.
        """
        for script in scripts:
            self.validname(script, check=True)
            if path.sep in script:
                raise ValueError(f'{script}: contains {path.sep}')
        with TemporaryDirectory() as tmpdir:
            fnames = []
            ctimes = []
            for script in scripts:
                fname = path.join(tmpdir, script)
                with open(fname, 'w', encoding='utf8') as file:
                    try:
                        file.write(self.getscript(script))
                    except SieveOperationError as err:
                        if not create:
                            raise
                        if not err.code:
                            if self.scriptexists(script):
                                raise
                        elif not err.matches('nonexistent'):
                            raise
                fnames.append(fname)
                ctimes.append(os.stat(fname).st_ctime)
            while True:
                cp = subprocess.run(command + fnames, *args,    # nosec B603
                                    check=check, **kwargs)
                retry: list[tuple[str, str, float]] = []
                for script, fname, ctime in zip(scripts, fnames, ctimes):
                    if os.stat(fname).st_ctime > ctime:
                        with open(fname, 'rb') as file:
                            try:
                                self.withreopen(self.putscript, file, script)
                            except SieveOperationError as err:
                                if errh is None:
                                    raise
                                if errh(err):
                                    retry.append((script, fname, ctime))
                if not retry:
                    return cp
                scripts, fnames, ctimes = map(list, zip(*retry))
        # NOTREACHED

    def getactive(self) -> Optional[str]:
        """Get the name of the active script."""
        for name, active in self.listscripts():
            if active:
                return name
        return None

    def getscript(self, script: str) -> str:
        """Download `script`.

        For example:

        >>> with open('foo.sieve', 'w', encoding='utf8') as file:
        >>>     file.write(mgr.getscript(file.name))

        Arguments:
            script: Script name.
        """
        self.validname(script, check=True)
        try:
            # pylint: disable=unbalanced-tuple-unpacking
            _, ((content,),) = self.execute('GETSCRIPT', script)
        except ValueError as err:
            raise SieveProtocolError('unexpected data') from err
        if isinstance(content, ACAPAtom):
            raise SieveProtocolError('unexpected atom')
        if isinstance(content, str):
            return content
        raise SieveProtocolError('unexpected ' + type(content).__name__)

    def havespace(self, script: str, size: int):
        """Check whether there is enough space for `script`.

        Arguments:
            script: Script name.
            size: Script size in bytes.

        Raises:
            SieveOperationError: *Not* enough space.
        """
        self.validname(script, check=True)
        self.execute('HAVESPACE', script, size)

    def listscripts(self, cached: bool = False) -> list[tuple[str, bool]]:
        """List scripts and whether they are the active script.

        For example:

        >>> mgr.listscripts()
        [('foo.sieve', False), ('bar.sieve', True)]

        >>> scripts = [script for script, _ in mgr.listscripts()]

        Arguments:
            cached: Return cached response? [#cache]_

        Returns:
            A list of script name/status tuples.

        .. [#cache] The cache is updated whenever a
                    state-changing operation is requested.
        """
        if not cached or self._scripts is None:
            self._scripts = []
            for line in self.execute('LISTSCRIPTS')[1]:
                try:
                    name = line[0]
                except IndexError as err:
                    raise SieveProtocolError('no data') from err
                if not isinstance(name, str):
                    raise SieveProtocolError('expected string')
                try:
                    status = line[1]
                    if not isinstance(status, str):
                        raise SieveProtocolError('expected string')
                    active = status.casefold() == 'active'
                except IndexError:
                    active = False
                self._scripts.append((name, active))
        return self._scripts

    def logout(self):
        """Log out.

        .. note::
            :meth:`logout` should be called to close the connection
            unless :class:`SieveManager` is used as a context manager.

        .. warning::
            Logging out is unsafe after a :exc:`ProtocolError`.
            Use :meth:`shutdown` instead.
        """
        if self.sock is not None:
            try:
                self.execute('LOGOUT')
                self.logger.info('logged out')
            except (OperationError, ProtocolError):
                try:
                    self.shutdown()
                except OSError:
                    pass
            except ConnectionError:
                pass
        self.close()

    def noop(self, tag: Optional[str] = None) -> Optional[str]:
        """Request a no-op.

        For example:

        >>> mgr.noop('foo')
        'foo'

        Arguments:
            tag: String for the server to echo back.

        Returns:
            Server echo.

        Raises:
            SieveCapabilityError: "NOOP" not supported.
        """
        assert self.capabilities    # nosec B101
        if not self.capabilities.version:
            raise SieveCapabilityError('NOOP: not supported')
        args = () if tag is None else (tag,)
        res, _ = self.execute('NOOP', *args)
        try:
            data = res.code[1]
        except IndexError:
            return None
        if isinstance(data, ACAPAtom) or not isinstance(data, str):
            raise SieveProtocolError('expected string')
        return data

    def putscript(self, source: Union[str, IO], target: str,
                  backup: Optional[BackupPol] = None):
        """Upload `source` to the server as `target`.

        For example:

        >>> mgr.putscript('# empty', 'foo.sieve')

        >>> with open('foo.sieve', 'br') as file:
        >>>     mgr.putscript(file, 'foo.sieve')

        Arguments:
            source: Script.
            target: Script name.
            backup: Backup policy (default: :attr:`backup`).

        .. important::
            Sieve scripts must be encoded in UTF-8.
        """
        self.validname(target, check=True)
        try:
            policy = self.backup if backup is None else backup
            self.backupscript(target, policy=policy)
        except SieveOperationError as err:
            if not err.code:
                for script, _ in self.listscripts():
                    if script == target:
                        raise
            elif not err.matches('nonexistent'):
                raise
        self._scripts = None
        self.execute('PUTSCRIPT', target, source)
        self.logger.info('uploaded %s', target)

    def renamescript(self, source: str, target: str, emulate: bool = True):
        """Rename `source` to `target`.

        Some servers do not the support the "RENAMESCRIPT" command.
        On such servers, renaming may be emulated by downloading `source`,
        re-uploading it as `target`, setting `target` to the active script
        if `source` is the active script, and then deleting `source`.

        For example:

        >>> mgr.renamescript('foo.sieve', 'bar.sieve', emulate=False)

        Arguments:
            source: Script name.
            target: Script name.
            emulate: Emulate "RENAMESCRIPT" if the server does not support it?

        Raises:
            SieveCapabilityError: "RENAMESCRIPT" not supported. [#emulate]_
            SieveOperationError: `source` does not exist or `target` exists.

        .. [#emulate] Only raised if `emulate` is `False`.
        """
        assert self.capabilities    # nosec B101
        self.validname(source, check=True)
        self.validname(target, check=True)
        if self.capabilities.version:
            self._scripts = None
            self.execute('RENAMESCRIPT', source, target)
            self.logger.info('renamed %s to %s', source, target)
        elif emulate:
            sourceactive: Optional[bool] = None
            for script, active in self.listscripts():
                if script == source:
                    sourceactive = active
                if script == target:
                    raise SieveOperationError(
                        code=(ACAPAtom('alreadyexists'),),
                        message=f'{target}: {os.strerror(EEXIST)}'
                    )
            if sourceactive is None:
                raise SieveOperationError(
                    code=(ACAPAtom('nonexistent'),),
                    message=f'{source}: {os.strerror(ENOENT)}'
                )
            self.copyscript(source, target, backup=BackupPol.NONE)
            if sourceactive:
                self.setactive(target)
            self.deletescript(source)
        else:
            raise SieveCapabilityError('RENAMESCRIPT: not supported')

    def scriptexists(self, script: str, cached: bool = False) -> bool:
        """Check if `script` exists.

        Arguments:
            cached: Return cached response? [#cache]_
        """
        self.validname(script, check=True)
        for name, _ in self.listscripts(cached=cached):
            if name == script:
                return True
        return False

    def setactive(self, script: str):
        """Mark `script` as the active script."""
        self.validname(script, check=True)
        self._scripts = None
        self.execute('SETACTIVE', script)
        self.logger.info('activated %s', script)

    def unauthenticate(self):
        """Unauthenticate.

        Raises:
            SieveCapabilityError: "UNAUTHENTICATE" not supported.
        """
        assert self.capabilities    # nosec B101
        if not self.capabilities.unauthenticate:
            raise SieveCapabilityError('UNAUTHENTICATE: not supported')
        self.execute('UNAUTHENTICATE')
        self.login = ''
        self.owner = ''
        self.logger.info('un-authenticated')

    def unsetactive(self):
        """Deactivate the active script."""
        self._scripts = None
        self.execute('SETACTIVE', '')
        self.logger.info('deactivated active script')

    @classmethod
    def validname(cls, script: str, check: bool = False) -> bool:
        """Check whether `script` is a valid script name.

        Raises:
            ValueError: `script` is *not* valid and `check` is `True`.
        """
        if cls._matchname(script):
            return True
        if check:
            raise ValueError(ctrl_escape(script) + ': bad name')
        return False

    backup: BackupPol = BackupPol.NONE
    """Default backup policy."""

    _matchname: Callable[..., Optional[re.Match]] = re.compile(
            '[^\u0000-\u001f\u0080-\u009f\u2028\u2029]+'
    ).fullmatch
    """Check whether a string is a valid script name.

    .. seealso::
        :RFC:`5198` (sec. 2)
            Definition of unicode format for network interchange.
        :RFC:`5804` (sec. 1.6)
            ManageSieve script names.
    """

    _scripts: Optional[list[tuple[str, bool]]] = None
    """Scripts returned by the last :meth:`listscripts`."""


def cert_getauthinfo(cert: 'x509') -> tuple[list[str], list[str]]:
    """Get information about the authority that issued `cert`.

    Returns:
        CA issuer URLs and OCSP responder base URLs.
    """
    exts = cert.extensions.get_extension_for_class(AuthorityInformationAccess)
    issuers = []
    responders = []
    for field in exts.value:
        oid = field.access_method
        if oid == AuthorityInformationAccessOID.CA_ISSUERS:
            issuers.append(field.access_location.value)
        elif oid == AuthorityInformationAccessOID.OCSP:
            responders.append(field.access_location.value)
    return issuers, responders


def cert_revoked(cert: 'x509') -> bool:
    """Check if `cert` has been revoked.

    Raises:
        OCSPDataError: `cert` contains no authority information.
        OCSPOperationError: no authoritative response.

    .. seealso::
        :RFC:`5019`
            Lightweight OCSP Profile
        :RFC:`6960`
            Online Certificate Status Protocol (OCSP)
    """
    try:
        issuers, responders = cert_getauthinfo(cert)
    except ExtensionNotFound as err:
        raise OCSPDataError('no authority information') from err
    for caurl in issuers:
        try:
            der = http_get(caurl)
        except (urllib.error.URLError, HTTPError) as err:
            logging.error(err)
            continue
        ca = x509.load_der_x509_certificate(der)
        builder = ocsp.OCSPRequestBuilder()
        # The use of SHA1 is mandated by RFC 5019.
        req = builder.add_certificate(cert, ca, SHA1()).build()  # nosec B303
        # pylint: disable=redefined-outer-name
        path = b64encode(req.public_bytes(Encoding.DER)).decode('ascii')
        for responder in responders:
            statusurl = urllib.parse.urljoin(responder, path)
            try:
                res = ocsp.load_der_ocsp_response(http_get(statusurl))
            except HTTPError as err:
                logging.error(err)
                continue
            if res.response_status == OCSPResponseStatus.SUCCESSFUL:
                now = datetime.datetime.now()
                if res.this_update <= now < res.next_update:
                    if res.certificate_status == OCSPCertStatus.REVOKED:
                        return True
                    if res.certificate_status == OCSPCertStatus.GOOD:
                        return False
    raise OCSPOperationError('no authoritative OCSP response')


# pylint: disable=redefined-outer-name
def ctrl_escape(string: str) -> str:
    """Escape control characters."""
    categories = map(unicodedata.category, string)
    escaped = [fr'\u{ord(char):04x}' if cat.startswith('C') else char
               for char, cat in zip(string, categories)]
    return ''.join(escaped)


def elems_randomise(elems: Sequence[T], weights: Iterable[int]) -> list[T]:
    """Randomise the order of `elems`."""
    nelems = len(elems)
    weights = list(weights)
    indices = list(range(len(elems)))
    randomised = []
    for _ in range(nelems):
        i, = random.choices(indices, weights, k=1)  # nosec B311
        randomised.append(elems[i])
        weights[i] = 0
    return randomised


def file_getsize(file: IO) -> int:
    """Get the size of file-like object relative to the current position."""
    try:
        pos = file.tell()
    except io.UnsupportedOperation:
        pos = 0
    try:
        size = os.fstat(file.fileno()).st_size - pos
    except io.UnsupportedOperation:
        size = file.seek(0, SEEK_END) - pos
        file.seek(pos, SEEK_SET)
    return size


def http_get(url: str) -> bytes:
    """Download a file from `url`.

    Raises:
        HTTPUsageError: `url` is not an HTTP URL.
        HTTPOperationError: "GET" failed.
    """
    while True:
        if not url.startswith('http://'):
            raise HTTPUsageError(f'{url}: not an HTTP URL')
        with urllib.request.urlopen(url) as res:  # nosec B310
            if res.status == 200:
                return res.read()
            if res.status in (301, 302, 303, 307, 308):
                if url := res.getheader('Location'):
                    continue
        raise HTTPOperationError(f'GET {url}: {res.reason}')
    # NOTREACHED


# pylint: disable=redefined-outer-name
def ipaddr_is(string: str) -> bool:
    """Check whether `string` is an IP address."""
    try:
        ipaddress.ip_address(string)
    except ValueError:
        return False
    return True


def srv_resolve(host: str) -> Iterator[SRV]:
    """Resolve a SRV record.

    Arguments:
        host: Hostname (e.g., :file:`_sieve._tcp.imap.foo.example`)

    Returns:
        An iterator over `SRV` records sorted by their priority
        and randomised according to their weight.

    .. note::
        Requires dnspython_.

    .. _dnspython: https://www.dnspython.org/
    """
    answer = dns.resolver.resolve(host, 'SRV')
    hosts: dict[str, list] = defaultdict(list)
    byprio: dict[int, list[SRV]] = defaultdict(list)
    for rec in answer.response.additional:
        name = rec.name.to_text()
        addrs = [i.address for i in rec.items if i.rdtype == dns.rdatatype.A]
        hosts[name].extend(addrs)
    for rec in answer:
        name = rec.target.to_text()
        if addrs := hosts.get(name):    # type: ignore[assignment]
            srvs = [SRV(rec.priority, rec.weight, a, rec.port) for a in addrs]
            byprio[rec.priority].extend(srvs)
        else:
            srv = SRV(rec.priority, rec.weight, name.rstrip('.'), rec.port)
            byprio[rec.priority].append(srv)
    for prio in sorted(byprio.keys()):
        srvs = byprio[prio]
        weights = [s.weight for s in srvs]
        if sum(weights) > 0:
            yield from elems_randomise(srvs, weights)
        else:
            yield from srvs


#
# SieveManager Shell
#

class ConfirmEnum(enum.IntEnum):
    """Confirmation types."""

    NO = 0
    YES = 1
    ALL = 2
    NONE = 3

    def __bool__(self) -> bool:
        return self in (self.YES, self.ALL)


class ShellPattern(str):
    """:class:`BaseShell` pattern."""

    def __add__(self, other):
        return self.__class__(super().__add__(other))

    def __radd__(self, other):
        return self.__class__(other.__add__(self))


ShellWord = Union['ShellPattern', str]
"""Alias for :class:`ShellPattern` and `str`."""


class BaseShell():
    """Base class for shells."""

    def __init__(self, clobber: bool = True,
                 encoding: str = ENCODING):
        """Initialise a :class:`BaseShell` object."""
        self._completions: list[str] = []
        self.clobber = clobber
        self.commands = tuple(a[3:] for a in dir(self) if a.startswith('do_'))
        self.encoding = encoding

    @staticmethod
    def getusage(func: Callable) -> Optional[str]:
        """Derive a usage message from a callable's docstring."""
        if docstring := func.__doc__:
            usage, _ = docstring.split('-', maxsplit=1)
            return 'usage: ' + usage.strip()
        return None

    def complete(self, text, n) -> Optional[str]:
        """Return the `n`-th completion for `text`.

        Tab-completion for commands is built-in, but tab-completion for
        arguments must be enabled by provided a :samp:`complete_{command}`
        method for each `command`. That command must return a list of
        :class:`str`-:class:`bool` pairs, where the boolean value
        controls whether a space is appended to the word.
        """
        if n == 0:
            logger = self.logger
            loglevel = logger.level
            logger.setLevel(logging.ERROR)
            try:
                begin = readline.get_begidx()
                buffer = readline.get_line_buffer()
                args = self.split(buffer[:begin])
                argidx = len(args)
                if argidx == 0:
                    words = [c + ' ' for c in self.commands]
                else:
                    command = 'help' if args[0] == '?' else args[0]
                    try:
                        complete = getattr(self, f'complete_{command}')
                    except AttributeError:
                        return None
                    words = [word + ' ' if addspace else word
                             for word, addspace in complete(argidx, text)]
                self._completions = sorted(fnmatch.filter(words, text + '*'))
            finally:
                logger.setLevel(loglevel)
        try:
            return self._completions[n]
        except (AttributeError, IndexError):
            return None

    def confirm(self, prompt: str, default: ConfirmEnum = ConfirmEnum.NO,
                multi: bool = False) -> ConfirmEnum:
        """Prompt for confirmation.

        Arguments:
            prompt: Prompt.
            default: Choice to return if no answer is given.
            multi: Give choices "all" and "none"?
        """
        fd = os.open('/dev/tty', os.O_RDWR | os.O_CLOEXEC)
        tty = os.fdopen(fd, encoding=self.encoding)
        try:
            while True:
                os.write(fd, prompt.encode(self.encoding) + b' ')
                answer = tty.readline().strip().casefold()
                if answer == '':
                    return default
                if answer in ('y', 'yes'):
                    return ConfirmEnum.YES
                if answer in ('n', 'no'):
                    return ConfirmEnum.NO
                if multi:
                    if answer == 'all':
                        return ConfirmEnum.ALL
                    if answer == 'none':
                        return ConfirmEnum.NONE
                if multi:
                    os.write(fd, b'Enter "yes", "no", "all", or "none"\n')
                else:
                    os.write(fd, b'Enter "yes" or "no"\n')
            # NOTREACHED
        finally:
            tty.close()

    def enter(self):    # noqa: C901
        """Start reading commands from standard input.

        The shell is exited if the end-of-filer marker is
        reached or a command raises :exc:`StopIteration`.

        Commands must be provided by :samp:`do_{command}` methods.
        "?" is used as alias for "help", "!" as alias for "shell".

        .. note::
            Entering the shell binds :kbd:`tab` to :func:`rl_complete`
            if :mod:`readline` is implemented by `libedit`.
        """
        logger = self.logger
        if self.hasatty:
            oldcompleter = readline.get_completer()
            readline.set_auto_history(True)
            readline.set_completer(self.complete)
            readline.parse_and_bind('tab: complete')
            if 'libedit' in readline.__doc__:
                readline.parse_and_bind('python:bind ^I rl_complete')
            logger.info('enter "? [command]" for help and "exit" to exit')
        try:
            while True:
                try:
                    prompt = self.getprompt() if self.hasatty else None
                    line = input(prompt).strip()
                except EOFError:
                    break
                try:
                    self.executeline(line)
                except StopIteration:
                    break
                except (ConnectionError, DNSError, ProtocolError):
                    raise
                # pylint: disable=broad-exception-caught
                except Exception as err:
                    if not self.hasatty:
                        raise
                    if isinstance(err, subprocess.CalledProcessError):
                        logger.error('%s: exited with status %d',
                                     err.cmd[0], err.returncode)
                        self.retval = 1
                    elif isinstance(err, (FileNotFoundError, FileExistsError)):
                        logger.error('%s: %s', err.filename,
                                     os.strerror(err.errno))
                        self.retval = 1
                    elif isinstance(err, OperationError):
                        for line in str(err).splitlines():
                            logger.error('%s', line)
                        self.retval = 1
                    elif isinstance(err, (GetoptError, UsageError)):
                        logger.error('%s', err)
                        self.retval = 2
                    elif isinstance(err, (Error, OSError, ValueError)):
                        logger.error('%s', err)
                        self.retval = 1
                    else:
                        raise
        finally:
            if self.hasatty:
                readline.set_completer(oldcompleter)

    def expand(self, words: Sequence['ShellWord']) -> list[str]:
        """Expand :class:`ShellPattern`-s using completions."""
        fnmatchcase = fnmatch.fnmatchcase
        expanded: list[str] = []
        try:
            getfiles = getattr(self, f'complete_{words[0]}')
        except (IndexError, AttributeError):
            getfiles = None
        for i, word in enumerate(words):
            if isinstance(word, ShellPattern) and getfiles:
                matches = sorted(f for f, _ in getfiles(i, '')
                                 if (not f.startswith('.')
                                     and fnmatchcase(f, word)))
                if not matches:
                    raise ValueError(f'{word}: no matches')
                expanded.extend(matches)
            else:
                word = re.sub(r'\\([*?\[\]])', r'\1', word)
                expanded.append(word)
        return expanded

    def getprompt(self) -> str:
        """Get a shell prompt."""
        return '> '

    def execute(self, command: str, *args: str) -> int:
        """Execute `command`.

        Returns:
            `command`'s return value.

        Raises:
            ShellUsageError: Command not found or arguments invalid.
        """
        try:
            method = getattr(self, f'do_{command}')
        except AttributeError as err:
            raise ShellUsageError(f'{command}: no such command') from err
        try:
            inspect.signature(method).bind(*args)
        except TypeError as err:
            raise ShellUsageError(self.getusage(method)) from err
        self.retval = 0 if (retval := method(*args)) is None else retval
        return self.retval

    def executeline(self, line: str) -> int:
        """Execute `line` as a :class:`BaseShell` command.

        Raises:
            ShellUsageError: Command not found or arguments invalid.
        """
        if line.startswith('?'):
            comm = 'help'
            args = self.split(line[1:])
        elif line.startswith('!'):
            comm = 'sh'
            args = self.split(line[1:])
        else:
            try:
                comm, *args = self.expand(self.split(line))
            except ValueError:
                return 0
        return self.execute(comm, *args)

    def runscript(self, script: TextIO) -> int:
        """Run `script`."""
        for line in script:
            self.retval = self.executeline(line.strip())
        return self.retval

    def split(self, line: str) -> list['ShellWord']:    # noqa C901
        """Split `line` into words as a POSIX-compliant shell would."""
        buffer: ShellWord = ''
        escape: bool = False
        quotes: list[str] = []
        tokens: list[ShellWord] = []
        for i, char in enumerate(line):
            if escape:
                if char in '*?[]':
                    buffer += '\\'
                buffer += char
                escape = False
            elif quotes:
                if char in '\'"':
                    if char == quotes[-1]:
                        del quotes[-1]
                    else:
                        quotes.append(char)
                    if quotes:
                        buffer += char
                elif char == '\\':
                    escape = True
                else:
                    if char in '*?[]':
                        buffer += '\\'
                    buffer += char
            elif char == '\\':
                escape = True
            elif char in '\'"':
                quotes.append(char)
            elif char.isspace():
                if i > 0 and not line[i - 1].isspace():
                    tokens.append(buffer)
                    buffer = ''
            elif char == '#':
                break
            else:
                if char in '*?[]':
                    buffer = ShellPattern(buffer)
                buffer += char
        if buffer:
            tokens.append(buffer)
        return tokens

    clobber: bool
    """Overwrite files?"""

    commands: tuple[str, ...]
    """Shell commands."""

    logger: logging.Logger = logging.getLogger(__name__)
    """Logger.

    :class:`SieveShell` logs messages of the following priorities:

    * :const:`logging.ERROR`
    * :const:`logging.INFO`
    """

    encoding: str
    """Encoding used for input/output."""

    hasatty: bool = os.isatty(sys.stdin.fileno())
    """Is standard input a terminal?"""

    retval: int = 0
    """Return value of the most recently completed command."""


# pylint: disable=too-many-public-methods
class SieveShell(BaseShell):
    """Shell around a `SieveManager` object."""

    def __init__(self, manager: 'SieveManager', *args,
                 confirm: ShellCmd = ShellCmd.ALL, **kwargs):
        """Initialise a :class:`SieveShell` object.

        Arguments:
            manager: Connection to a ManageSieve server.
            confirm: Shell commands that require confirmation if
                they overwrite or remove files.
        """
        super().__init__(*args, **kwargs)
        self.confirmcmds = confirm
        self.manager = manager

    # Methods
    def getprompt(self, *args, **kwargs):
        prompt = super().getprompt(*args, **kwargs)
        mgr = self.manager
        if mgr and (url := mgr.geturl()) is not None:
            prompt = ('' if mgr.tls else '(insecure) ') + str(url) + prompt
        return prompt

    def execute(self, *args, **kwargs) -> int:
        retval = super().execute(*args, **kwargs)
        if warning := self.manager.warning:
            for line in warning.splitlines():
                self.logger.warning('%s', ctrl_escape(line))
        return retval

    def editscripts(self, editor: list[str], *args: str):
        """Edit scripts with the given `editor`."""
        mgr = self.manager

        def retry(err: Exception) -> bool:
            print(str(err).rstrip('\r\n'), file=sys.stderr)
            return bool(self.confirm('Re-edit?', default=ConfirmEnum.YES))

        (opts, scripts) = getopt(list(args), 'a')
        for opt, _ in opts:
            if opt == '-a':
                scripts = [active] if (active := mgr.getactive()) else []
        if scripts:
            mgr.editscripts(editor, list(scripts), errh=retry)
        else:
            self.logger.error('no scripts given')

    # Shell commands
    @staticmethod
    def do_about():
        """about - show information about SieveManager"""
        print(ABOUT.strip())
        print('Home page: https://odkr.codeberg.page/sievemgr')

    def do_activate(self, script: str):
        """activate script - mark script as active"""
        self.manager.setactive(script)

    def do_caps(self):
        """caps - show server capabilities"""
        print('---')
        if (caps := self.manager.capabilities) is not None:
            for key, value in caps.__dict__.items():
                if not value:
                    continue
                if key in ('implementation', 'language',
                           'maxredirects', 'owner', 'version'):
                    if isinstance(value, str):
                        value = yaml_escape(value)
                    print(f'{key}: {value}')
                elif key in ('notify', 'sasl', 'sieve'):
                    items = [yaml_escape(i) if isinstance(i, str) else str(i)
                             for i in value]
                    # pylint: disable=consider-using-f-string
                    print('{}: [{}]'.format(key, ', '.join(items)))
                elif key in ('starttls', 'unauthenticate'):
                    print(f'{key}: yes')
            for key, value in caps.notunderstood.items():
                if value is not None:
                    if isinstance(value, str):
                        value = yaml_escape(value)
                    print(f'{key}: {value}')
        print('...')

    def do_cat(self, *scripts: str):
        """cat [script ...] - concatenate scripts on standard output"""
        for script in scripts:
            sys.stdout.write(self.manager.getscript(script))

    def do_cd(self, localdir: str = HOMEDIR):
        """cd [localdir] - change local directory"""
        os.chdir(localdir)
        self.logger.info('changed directory to %s', localdir)

    def do_cert(self):
        """cert - show the server's TLS certificate."""
        indent = ' ' * 4
        mgr = self.manager
        if not isinstance(mgr.sock, ssl.SSLSocket):
            raise ShellUsageError('not a secure connection')
        cert = mgr.sock.getpeercert()
        assert cert     # nosec B101
        value: Any
        print('---')
        for key, value in sorted(cert.items()):
            if key in ('OCSP', 'caIssuers', 'crlDistributionPoints'):
                print(f'{key}:')
                for item in sorted(value):
                    if isinstance(item, str):
                        item = yaml_escape(item)
                    print(f'{indent}- {item}')
            elif key in ('issuer', 'subject'):
                print(f'{key}:')
                for pairs in sorted(value):
                    print(f'{indent}- ', end='')
                    for i, (k, v) in enumerate(pairs):
                        if isinstance(v, str):
                            v = yaml_escape(v)
                        if i == 0:
                            print(f'{k}: {v}')
                        else:
                            print(f'{indent}  {k}: {v}\n')
            elif key == 'subjectAltName':
                print(f'{key}:')
                for k, v in sorted(value):
                    v = yaml_escape(v)
                    print(f'{indent}- {k}: {v}')
            elif isinstance(value, str):
                value = yaml_escape(value)
                print(f'{key}: {value}')
            elif isinstance(value, int):
                print(f'{key}: {value}')
        print('...')

    def do_check(self, localscript: str):
        """check localscript - check whether localscript is valid"""
        with open(localscript, 'rb', encoding='utf8') as file:
            # checkscript performs a blocking send/sendline,
            # but holding a lock should be okay in an interactive app.
            fcntl.flock(file.fileno(), LOCK_SH | LOCK_NB)
            self.manager.checkscript(file)
        self.logger.info('%s is valid', localscript)

    def do_cmp(self, *args: str) -> int:
        """cmp [-s] script1 [...] scriptN - compare scripts"""
        silent = False
        opts, scripts = getopt(list(args), 's')
        for opt, _ in opts:
            if opt == '-s':
                silent = True
        if len(scripts) < 2:
            message = self.getusage(self.do_cmp)
            assert message  # nosec B101
            raise ShellUsageError(message)
        prefix = ', '.join(scripts)
        contents = map(self.manager.getscript, scripts)
        iters = map(str.splitlines, contents)
        for i, lines in enumerate(itertools.zip_longest(*iters), start=1):
            for j, chars in enumerate(itertools.zip_longest(*lines), start=1):
                char1 = chars[0]
                for char2 in chars[1:]:
                    if char1 != char2:
                        if not silent:
                            print(f'{prefix}: line {i}, column {j} differs')
                        return 1
        if not silent:
            print(f'{prefix}: equal')
        return 0

    def do_cp(self, *args: str):
        """cp [-f|-i] source target - re-upload source as target"""
        clobber = self.clobber
        confirm = bool(self.confirmcmds & ShellCmd.CP)
        opts, scripts = getopt(list(args), 'Cfi')
        for opt, _ in opts:
            if opt == '-f':
                clobber = True
                confirm = False
            elif opt == '-i':
                clobber = True
                confirm = True
        try:
            source, target = scripts
        except ValueError as err:
            message = self.getusage(self.do_cp)
            assert message  # nosec B101
            raise ShellUsageError(message) from err
        if self.manager.scriptexists(target):
            if not clobber:
                raise FileExistsError(EEXIST, os.strerror(EEXIST), target)
            if confirm and not self.confirm(f'Overwrite {target}?'):
                return
        self.manager.copyscript(source, target)

    def do_deactivate(self):
        """deactivate - deactivate the active script"""
        self.manager.unsetactive()

    def do_diff(self, *args: str) -> int:
        """diff <options> script1 script2 - show how scripts differ"""
        pairs, scripts = getopt(list(args), 'C:U:bcu')
        opts = tuple(filter(bool, itertools.chain(*pairs)))
        if len(scripts) != 2:
            message = self.getusage(self.do_diff)
            assert message  # nosec B101
            raise ShellUsageError(message)
        cp = self.manager.editscripts(['diff', *opts], scripts, check=False)
        return cp.returncode

    def do_echo(self, *args: str):
        """echo word [...] - print words to standard output."""
        print(*args)

    def do_ed(self, *args: str):
        """ed [-a] script [...] - edit scripts with a line editor"""
        self.editscripts(EDITOR, *args)

    def do_exit(self) -> bool:
        """exit - exit SieveManager"""
        raise StopIteration()

    def do_get(self, *args: str):   # noqa C901
        """get [-a] [-f|-i] [-o file] [script ...] - download script"""
        mgr = self.manager
        opts, sources = getopt(list(args), 'afio:')
        output = ''
        clobber = self.clobber
        confirm = bool(self.confirmcmds & ShellCmd.GET)
        multi = len(sources) > 1
        for opt, arg in opts:
            if opt == '-a':
                sources = [active] if (active := mgr.getactive()) else []
            elif opt == '-f':
                clobber = True
                confirm = False
            elif opt == '-i':
                clobber = True
                confirm = True
            elif opt == '-o':
                if multi:
                    raise ShellUsageError('-o: too many sources')
                output = arg
        answer = ConfirmEnum.NO if confirm else ConfirmEnum.ALL
        backup = mgr.backup
        for src in sources:
            # Try not to make a backup if the source doesn't exist.
            # Writing to a temporary file would create a worse race condition.
            if backup != BackupPol.NONE:
                if not mgr.scriptexists(src):
                    raise FileNotFoundError(ENOENT, os.strerror(ENOENT), src)
            targ = output if output else src
            flags = O_CREAT | O_EXCL | O_WRONLY | O_TRUNC
            if path.exists(targ):
                if not clobber:
                    raise FileExistsError(EEXIST, os.strerror(EEXIST), targ)
                if answer not in (ConfirmEnum.ALL, ConfirmEnum.NONE):
                    answer = self.confirm(f'Overwrite {targ}?', multi=multi)
                if answer:
                    def getfiles():
                        # pylint: disable=cell-var-from-loop
                        return dir_read(path.dirname(targ), path.isfile)
                    if bak := BackupPol.getfname(backup, targ, getfiles):
                        shutil.copy(targ, bak)
                        self.logger.info('backed up %s as %s', targ, bak)
                    flags &= ~O_EXCL
                else:
                    continue
            fd = os.open(targ, flags, mode=0o644)
            # getscript performs a blocking read,
            # but holding a lock should be okay in an interactive app.
            fcntl.flock(fd, LOCK_SH | LOCK_NB)
            with os.fdopen(fd, 'w', encoding='utf8') as file:
                file.write(mgr.getscript(src))
            self.logger.info('downloaded %s as %s', src, targ)

    def do_help(self, name: Optional[str] = None):
        """help [command] - list commands/show help for command"""
        if name:
            try:
                print(getattr(self, f'do_{name}').__doc__)
            except AttributeError:
                self.logger.error('%s: unknown command', name)
        else:
            words_columnise(self.commands)

    def do_ls(self, *args: str):
        """ls [-1al] [script ...] - list scripts"""
        active = False
        long = False
        one = not self.hasatty
        (opts, fnames) = getopt(list(args), '1al')
        for opt, _ in opts:
            if opt == '-1':
                one = True
            elif opt == '-a':
                active = True
            elif opt == '-l':
                long = True
        if active:
            if script := self.manager.getactive():
                print(script)
            else:
                self.logger.warning('no active script')
        else:
            scripts = self.manager.listscripts()
            if fnames:
                existing = {fname for fname, _ in scripts}
                for name in set(fnames) - existing:
                    raise FileNotFoundError(ENOENT, os.strerror(ENOENT), name)
                scripts = [s for s in scripts if s[0] in fnames]
            scripts.sort()
            if long:
                for fname, active in scripts:
                    print('a' if active else '-', fname)
                print('e')
            elif one:
                for script, _ in scripts:
                    print(script)
            else:
                words = [f + ('*' if a else '') for f, a in scripts]
                words_columnise(words)

    def do_more(self, *args: str):
        """more <options> script [...] - display scripts page-by-page."""
        mgr = self.manager
        pairs, scripts = getopt(list(args), 'aceip:s')
        opts = list(filter(bool, itertools.chain(*pairs)))
        if '-a' in opts:
            active = mgr.getactive()
            if active is None:
                self.logger.warning('no active script')
                return
            scripts = [active]
            opts.remove('-a')
        elif not scripts:
            message = self.getusage(self.do_more)
            assert message  # nosec B101
            raise ShellUsageError(message)
        mgr.editscripts(PAGER + opts, list(scripts))

    def do_mv(self, *args: str):
        """mv [-f|-i] source target - rename source to target"""
        clobber = self.clobber
        confirm = bool(self.confirmcmds & ShellCmd.MV)
        mgr = self.manager
        opts, scripts = getopt(list(args), 'Cfi')
        for opt, _ in opts:
            if opt == '-f':
                clobber = True
                confirm = False
            elif opt == '-i':
                clobber = True
                confirm = True
        try:
            source, target = scripts
        except ValueError as err:
            message = self.getusage(self.do_mv)
            assert message  # nosec B101
            raise ShellUsageError(message) from err
        if mgr.scriptexists(target):
            if not clobber:
                raise FileExistsError(EEXIST, os.strerror(EEXIST), target)
            if confirm and not self.confirm(f'Overwrite {target}?'):
                return
            mgr.backupscript(target)
            mgr.deletescript(target)
        mgr.renamescript(source, target, emulate=True)

    def do_put(self, *args: str):
        """put [-f|-i] [-a] [-o name] [localscript ...] - upload scripts"""
        active: Optional[str] = None
        clobber: bool = self.clobber
        confirm: bool = bool(self.confirmcmds & ShellCmd.PUT)
        mgr: SieveManager = self.manager
        output: str = ''
        putactive: bool = False
        opts, sources = getopt(list(args), 'Cafio:')
        multi = len(sources) > 1
        for opt, arg in opts:
            if opt == '-a':
                if multi:
                    raise ShellUsageError('-a: only one script can be active')
                active = mgr.getactive()
                putactive = True
                if active:
                    output = active
            elif opt == '-f':
                clobber = True
                confirm = False
            elif opt == '-i':
                clobber = True
                confirm = True
            elif opt == '-o':
                if multi:
                    raise ShellUsageError('-o: too many sources')
                output = arg
        answer = ConfirmEnum.NO if confirm else ConfirmEnum.ALL
        for src in sources:
            targ = output if output else src
            if mgr.scriptexists(targ):
                if not clobber:
                    raise FileExistsError(EEXIST, os.strerror(EEXIST), targ)
                if answer not in (ConfirmEnum.ALL, ConfirmEnum.NONE):
                    answer = self.confirm(f'Overwrite {targ}?', multi=multi)
                if not answer:
                    continue
            with open(src, 'rb', encoding='utf8') as file:
                # checkscript performs a blocking send/sendline,
                # but holding a lock should be okay in an interactive app.
                fcntl.flock(file.fileno(), LOCK_SH | LOCK_NB)
                mgr.putscript(file, targ)
            if putactive and targ != active:
                mgr.setactive(targ)

    def do_python(self):
        """python - enter Python read-evaluate-print loop"""
        try:
            readline.clear_history()
        except AttributeError:
            pass
        try:
            banner = (f'Python {sys.version}\n'
                       'enter "dir()" to list objects, '
                       '"help()" for help, and '
                       '"exit()" to exit')
            code.interact(local=ObjWrapper(self.manager),
                          banner=banner, exitmsg='')
        except SystemExit:
            pass

    def do_rm(self, *args: str):
        """rm [-f|-i] [script ...] - remove script"""
        confirm = bool(self.confirmcmds & ShellCmd.RM)
        opts, scripts = getopt(list(args), 'fi')
        for opt, _ in opts:
            if opt == '-f':
                confirm = False
            elif opt == '-i':
                confirm = True
        answer = ConfirmEnum.NO if confirm else ConfirmEnum.ALL
        multi = len(scripts) > 1
        for script in scripts:
            if answer is not ConfirmEnum.ALL:
                answer = self.confirm(f'Remove {script}?', multi=multi)
                if answer is ConfirmEnum.NONE:
                    self.logger.info('stopped')
                    break
            if answer:
                self.manager.deletescript(script)

    def do_sh(self, *args: str):
        """sh [command] [argument ...] - run system command or shell"""
        if not args:
            args = (pwd.getpwuid(os.getuid()).pw_shell,)
        subprocess.run(args, check=True)   # nosec B603

    def do_su(self, user: str):
        """su user - manage scripts of user."""
        mgr = self.manager
        login = mgr.login
        if mgr.login:
            mgr.unauthenticate()
        # Some ManageSieve servers reject the first
        # "AUTHENTICATE" after an "UNAUTHENTICATE".
        for i in range(2):
            try:
                mgr.authenticate(login, *mgr.auth,
                                 owner=user, sasl=mgr.sasl, **mgr.kwauth)
            except SieveOperationError:
                if i:
                    raise
                continue
            break

    def do_vi(self, *args: str):
        """vi [-a] script [...] - edit scripts with a visual editor"""
        self.editscripts(VISUAL, *args)

    def do_xargs(self, command: str, *args: str):
        """xargs cmd [arg ...] - call cmd with arguments from standard input"""
        try:
            func = getattr(self, f'do_{command}')
        except AttributeError as err:
            raise ShellUsageError(f'{command}: no such command') from err
        lines = []
        try:
            while line := sys.stdin.readline().rstrip('\n'):
                lines.append(line)
        except EOFError:
            pass
        return func(*args, *lines)

    # Globbing and tab-completion
    @staticmethod
    def complete_dirs(_: int, text: str) -> list[tuple[str, bool]]:
        """Complete local directory names."""
        return [(d + '/', False)
                for d in dir_read(path.dirname(text), path.isdir)]

    @staticmethod
    def complete_files(_: int, text: str) -> list[tuple[str, bool]]:
        """Complete local filenames."""
        return [(f + '/', False) if path.isdir(f) else (f, True)
                for f in dir_read(path.dirname(text))]

    def complete_scripts(self, *_) -> list[tuple[str, bool]]:
        """Complete script names."""
        return [(s, True) for s, _ in self.manager.listscripts(cached=True)]

    complete_activate = complete_scripts
    """Completer for activate."""

    complete_cat = complete_scripts
    """Completer for cat."""

    complete_cd = complete_dirs
    """Completer for cd."""

    complete_cmp = complete_scripts
    """Completer for cmp."""

    complete_cp = complete_scripts
    """Completer for cp."""

    complete_check = complete_files
    """Completer for check."""

    complete_diff = complete_scripts
    """Completer for diff."""

    complete_ed = complete_scripts
    """Completer for ed."""

    complete_get = complete_scripts
    """Completer for get."""

    def complete_help(self, *_) -> list[tuple[str, bool]]:
        """Completer for help."""
        return [(c, True) for c in self.commands]

    complete_ls = complete_scripts
    """Completer for ls."""

    complete_more = complete_scripts
    """Completer for more."""

    complete_mput = complete_files
    """Completer for mput."""

    complete_mv = complete_scripts
    """Completer for mv."""

    complete_put = complete_files
    """Completer for put."""

    complete_rm = complete_scripts
    """Completer for rm."""

    complete_vi = complete_scripts
    """Completer for vi."""

    # Properties
    manager: SieveManager
    """Connection to a ManageSieve server."""


class ObjWrapper(dict):
    """Object wrapper for use with :func:`code.interact`.

    Arguments:
        obj: Object to wrap.
    """

    def __init__(self, obj: Any):
        """Initialise a proxy."""
        super().__init__()
        self.obj = obj

    @staticmethod
    def exit():
        """Exit the Python read-evaluate-print loop."""
        raise SystemExit()

    def keys(self):
        """Same as :meth:`dir`."""
        return dir(self.obj) + list(globals().keys())

    def __getitem__(self, key):
        """Get an attribute from :attr:`obj` or func:`globals`."""
        try:
            return getattr(self, key)
        except AttributeError:
            pass
        try:
            return getattr(self.obj, key)
        except AttributeError:
            pass
        return globals()[key]

    obj: object
    """Wrapped object."""


def dir_read(dirname: str, predicate: Optional[Callable[[str], bool]] = None) \
        -> Iterator[str]:
    """Get every filename in `dirname` that matches `predicate`."""
    for dirent in os.listdir(dirname if dirname else '.'):
        fname = path.join(dirname, dirent)
        if predicate is None or predicate(fname):
            yield fname


def words_columnise(words: Sequence[str], file: TextIO = sys.stdout):
    """Print `words` in columns."""
    nwords = len(words)
    if nwords > 0:
        colwidth = max(map(len, words)) + 1
        maxncols = shutil.get_terminal_size().columns // colwidth
        nrows = math.ceil(nwords / maxncols)
        table: tuple[list[str], ...] = tuple([] for _ in range(nrows))
        for i, word in enumerate(words):
            table[i % nrows].append(word)
        for row in table:
            print(''.join([col.ljust(colwidth) for col in row]), file=file)


def yaml_escape(data: str):
    """Strip and quote `data` for use as YAML scalar if needed."""
    indicators = ('-', '?', ':', ',', '[', ']', '{', '}', '#', '&',
                  '*', '!', '|', '>', "'", '"', '%', '@', '`')
    data = data.strip()
    if not data:
        return '""'
    if data == 'no':
        return '"no"'
    if data[0] in indicators or ': ' in data or ' #' in data:
        return '"' + data.replace('\\', '\\\\').replace('"', '\\\"') + '"'
    return data


#
# Configuration
#

class AttrDict():
    """Access attribute as dictionary items.

    For example:

    >>> obj = AttrDict()
    >>> obj.foo = 'bar'
    >>> obj['foo']
    'bar'

    Methods and private properties are ignored.
    """

    def __getitem__(self, key: str) -> Any:
        """Get an attribute."""
        try:
            return getattr(self, key)
        except AttributeError as err:
            raise KeyError(key) from err

    def __iter__(self) -> Iterator[Any]:
        """Iterate over attribute names."""
        return self.keys()

    def get(self, key: str, default: Any = None) -> Any:
        """Get the value of an attribute, but fallback to a default."""
        try:
            return getattr(self, key)
        except AttributeError:
            pass
        return default

    def keys(self) -> Iterator[Any]:
        """Get attribute names."""
        for key in dir(self):
            if not key.startswith('_') and not callable(self.get(key)):
                yield key

    def items(self) -> Iterator[tuple[Any, Any]]:
        """Get name-value pairs."""
        for key in self.keys():
            yield (key, self.get(key))

    def values(self) -> Iterator[Any]:
        """Get attribute values."""
        for _, value in self.items():
            yield value


class AttrProxyStore(AttrDict):
    """Back-end for proxied attributes."""

    def __init__(self, *args, **kwargs):
        """Initialise a new attribute proxy back-end.

        Arguments are used to populate the back-end with values.

        Arguments:
            args: Passed to `dict` as positional arguments.
            kwargs: Passed to `dict` as keyword arguments.
        """
        self._store = dict(*args, **kwargs)

    def __or__(self, other):
        """Merge two :class:`AttrProxyStore` objects."""
        ret = self.__class__()
        ret._store = self._store | other._store
        return ret

    def __ior__(self, other):
        """Merge :class:`AttrProxyStore` objects in-place."""
        self._store |= other._store
        return self

    def __ror__(self, other):
        """Merge two :class:`AttrProxyStore` objects in reverse."""
        ret = self.__class__()
        ret._store = other._store | self._store
        return ret

    def __setitem__(self, key: str, value: str):
        """Set `key` to `value`.

        :raises KeyError: Bad attribute.
        """
        if key.startswith('_'):
            raise KeyError(key)
        try:
            attr = getattr(self, key)
        except AttributeError as err:
            raise KeyError(key) from err
        if callable(attr):
            raise KeyError(key)
        setattr(self, key, value)

    def isset(self, name: str) -> bool:
        """Check if the `name` attribute has been set."""
        if name not in dir(self):
            raise AttributeError(f'{name}: no such attribute')
        try:
            return self._store[name] is not None
        except KeyError:
            return False

    def _get(self, name):
        """Get a value from the back-end."""
        return self._store[name]

    def _set(self, name, value):
        """Set a value in the back-end."""
        self._store[name] = value


class AttrProxy(ABC):
    """Base class for attribute proxies.

    Allows to mangle an attribute's value when it is set or read.
    Can only be used with :class:`AttrProxyStore` subclasses.

    For example:

    >>> class Foo(AttrProxyStore):
    >>>     foo = BoolAttrProxy('foo', default=False)
    >>>     bar = IntAttrProxy('bar', default=0)
    >>>
    >>> foo = Foo(bar=1)
    >>> foo.foo
    False
    >>> foo.bar
    1
    >>> foo.foo = 'yes'
    >>> foo.foo
    True
    >>> foo.bar = '2'
    >>> foo.bar
    2
    """

    def __init__(self, name: str, default: Any = None):
        """Initialise a proxied attribute.

        Arguments:
            name: Attribute name.
            default: Default.
        """
        self.name = name
        self.default = default

    def __get__(self, obj: AttrProxyStore, _cls: type) -> Any:
        """Get the attribute's value.

        Arguments:
            obj: Object to which the attribute belongs.

        Raises:
            ValueError: `value` is invalid.
        """
        try:
            return obj._get(self.name)
        except KeyError:
            return self.default

    def __set__(self, obj: AttrProxyStore, value: Optional[str]):
        """Set the attribute's value.

        Arguments:
            obj: Object to which the attribute belongs.
            value: Value to mangle.

        Raises:
            ValueError: `value` is invalid.
        """
        obj._set(self.name, self.default if value is None else value)


class ManglingAttrProxy(AttrProxy):
    """Base class for attribute proxies that mangle values."""

    @abstractmethod
    def mangle(self, obj: AttrProxyStore, value: Any) -> Any:
        """Mangle `value`.

        Arguments:
            obj: Object to which the attribute belongs.
            value: Value to mangle.

        Raises:
            ValueError: `value` is invalid.
        """


class ListAttrProxyTrait():
    """Trait for lists."""

    _splititems: Callable = re.compile(r'\s*,\s*').split
    """Split a comma-separated list into items."""


class EagerAttrProxy(ManglingAttrProxy):
    """Base class for 'eager' attribute proxies (mangled on write)."""

    def __set__(self, obj: AttrProxyStore, value: Any):
        if value == '':
            raise ValueError(f'{self.name}: empty')
        super().__set__(obj, self.mangle(obj, value))


class LazyAttrProxy(ManglingAttrProxy):
    """Base class for 'lazy' attribute proxies (mangled on read)."""

    def __get__(self, obj: AttrProxyStore, cls: type) -> Any:
        return self.mangle(obj, super().__get__(obj, cls))


class ExpAttrProxy(LazyAttrProxy):
    """Expand '~', environment variables, and attributes."""

    def expand(self, obj: AttrProxyStore, value: str) -> str:
        """Expand '~', environment variables, and attributes."""
        name = self.name
        template = string.Template(value)
        # template.get_identifiers is only available in Python >= v3.11.
        varnames = set()
        # pylint: disable=consider-using-f-string
        for pattern in (r'\$(%s)' % template.idpattern,
                        r'\$\{(%s)\}' % template.idpattern):
            for match in re.finditer(pattern, value, flags=re.IGNORECASE):
                varnames.add(match.group(1))
        vars_ = {}
        for varname in varnames:
            try:
                value = getattr(obj, varname)
            except AttributeError as err:
                raise ValueError(f'{name}: ${varname}: no such key') from err
            if value is None:
                raise ValueError(f'{name}: ${varname}: not set')
            if not isinstance(value, (int, str)):
                raise ValueError(f'{name}: ${varname}: not a scalar.')
            vars_[varname] = value
        return path.expanduser(template.substitute(vars_))

    def mangle(self, obj: AttrProxyStore,
               value: Optional[str]) -> Optional[str]:
        return None if value is None else self.expand(obj, value)


class NumAttrProxy(EagerAttrProxy):
    """Convert attribute to a number of type :attr:`cls`."""

    # pylint: disable=redefined-builtin
    def __init__(self, *args,
                 min: Optional[Union[int, float]] = None,
                 max: Optional[Union[int, float]] = None,
                 **kwargs):
        """Initialise the proxy.

        Arguments:
            name: Attribute name.
            min: Smallest permissible value.
            max: Greatest permissible value.
            default: Default.
        """
        super().__init__(*args, **kwargs)
        self.min = min
        self.max = max

    def mangle(self, _obj: Any,
               value: Union[int, float, str]) -> Union[int, float]:
        try:
            num = self.cls(value)
        except ValueError as err:
            raise ValueError(f'{self.name}: {value}: not a number') from err
        if self.min is not None and num < self.min:
            raise ValueError(f'{self.name}: {value} < {self.min}')
        if self.max is not None and num > self.max:
            raise ValueError(f'{self.name}: {value} > {self.max}')
        return num

    @property
    @abstractmethod
    def cls(self) -> type:
        """Numeric type to convert values to."""


class AuthAttrProxy(EagerAttrProxy, ListAttrProxyTrait):
    """Convert SASL mechanism names to objects."""

    def mangle(self, obj: AttrProxyStore,
               value: Union[Iterable[type[BaseAuth]], str]) \
                    -> list[type[BaseAuth]]:
        if isinstance(value, str):
            classes = BaseAuth.getmechs(obsolete=True)
            mechs = []
            for name in self._splititems(value.casefold()):
                matches = []
                for cls in classes:
                    if fnmatch.fnmatchcase(cls.name.casefold(), name):
                        if cls in mechs:
                            message = f'{self.name}: {cls.name}: duplicate'
                            raise ValueError(message)
                        matches.append(cls)
                if not matches:
                    raise ValueError(f'{self.name}: {name}: no matches')
                mechs.extend(matches)
        elif isinstance(value, Sequence):
            mechs = value     # type: ignore[assignment]
        if len(set(m.type for m in mechs)) > 1:
            raise ValueError(f'{self.name}: bad combination')
        return mechs

    superclasses = (BasePassAuth, ExternalAuth)
    """Authentication types."""


class BoolAttrProxy(EagerAttrProxy):
    """Convert "yes" and "no" to :class:`bool`."""

    def mangle(self, _obj: Any, value: Union[bool, str]) -> bool:
        if value in (True, 'yes'):
            return True
        if value in (False, 'no'):
            return False
        raise ValueError(f'{self.name}: {value}: not a boolean')


class CmdAttrProxy(ExpAttrProxy):
    """Split up attribute into a list using :func:`shlex.split`."""

    def mangle(self,    # type: ignore[override]
               obj: AttrProxyStore, value: Optional[str]) -> Optional[list]:
        if value is not None:
            return [self.expand(obj, word)
                    for word in shlex.split(value, posix=True)]
        return None


class EnumAttrProxy(EagerAttrProxy):
    """Convert attribute to an :class:`enum.Enum`."""

    def __init__(self, *args, cls: type[enum.Enum], **kwargs):
        """Initialise the proxy.

        Arguments:
            name: Attribute name.
            cls: Type to convert the value to.
            default: Default.
        """
        assert issubclass(cls, enum.Enum)    # nosec B101
        super().__init__(*args, **kwargs)
        self.cls = cls

    def mangle(self, obj: AttrProxyStore,
               value: Optional[Union[enum.Enum, str]]) -> Optional[enum.Enum]:
        if value is None:
            return None
        if isinstance(value, enum.Enum):
            return value
        for member in self.cls:
            if member.name.casefold() == value.casefold():
                return member
        raise ValueError(f'{self.name}: {value}: no such item')


class FlagAttrProxy(EnumAttrProxy, ListAttrProxyTrait):
    """Convert attribute to an :class:`int`."""

    def mangle(self, obj: AttrProxyStore,   # type: ignore[override]
               value: Optional[Union[str, int, enum.IntFlag]]) -> int:
        flag = 0
        if value is not None:
            if isinstance(value, int):
                flag = value
            elif isinstance(value, enum.IntFlag):
                flag = int(value)
            elif isinstance(value, str):
                for name in self._splititems(value):
                    for member in self.cls:
                        if name.casefold() == member.name.casefold():
                            flag |= member.value
                            break
                    else:
                        raise ValueError(f'{self.name}: {name}: no such item')
        return flag


class FloatAttrProxy(NumAttrProxy):
    """Convert attribute to a :class:`float`."""

    cls = float


class GlobalAttrProxy(BoolAttrProxy):
    """Set global variable when attribute is changed."""

    def __init__(self, *args, var: str, **kwargs):
        """Initialise the proxy.

        Arguments:
            name: Attribute name.
            default: Default.
            var: Variable name.
        """
        super().__init__(*args, **kwargs)
        self.var = var

    def mangle(self, obj: AttrProxyStore,
               value: str) -> bool:     # type: ignore[override]
        boolean = super().mangle(obj, value)
        globals()[self.var] = boolean
        return boolean


class HostAttrProxy(EagerAttrProxy):
    """Check whether value is a valid hostname."""

    def mangle(self, _obj: AttrProxyStore,
               value: Optional[str]) -> Optional[str]:
        if value is None:
            return self.default
        if (match := self._matchhost(value)) is not None:
            for host in match.groupdict().values():
                if host is not None:
                    return host
        raise ValueError(f'{self.name}: {value}: neither hostname nor address')

    _matchhost: Callable[..., Optional[re.Match]] = re.compile('|'.join((
        r'(?P<ipv4>([0-2]?[0-9]{1,2}\.){3}[0-2]?[0-9]{1,2})',
        r'\[(?P<ipv6>([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4})\]',
        r'(?P<hostname>([a-z0-9]([a-z0-9-]{0,61}[a-z0-9]\.)?)+)',
    )), flags=re.IGNORECASE).fullmatch
    """Matches hosts."""


class IntAttrProxy(NumAttrProxy):
    """Convert attribute to an integer."""

    cls = int


class Config(AttrProxyStore):
    """Configuration for the SieveManager command-line client."""

    @classmethod
    def fromfile(cls, fname: str) -> 'Config':
        """Read configuration from `fname`.

        See :doc:`docs/sieverc.rst` for the syntax.

        :raise ConfigError: Syntax error.
        """
        conf = cls()
        ptr = conf
        with open(fname, encoding='utf8') as file:
            for i, line in enumerate(file.readlines(), start=1):
                if line == '\n' or cls._matchcmnt(line):
                    continue
                try:
                    key, value = line.rstrip().split(maxsplit=1)
                    if key == 'account':
                        try:
                            login, host = value.rsplit('@', maxsplit=1)
                        except ValueError:
                            login, host = None, value
                        ptr = cls(host=host, login=login)
                        ptr._login = login
                        ptr._host = host
                        conf._sections.append(ptr)
                    else:
                        ptr[key] = value
                except KeyError as err:
                    message = f'{fname}:{i}: {key}: no such variable'
                    raise ShellConfigError(message) from err
                except (ValueError, Error) as err:
                    raise ShellConfigError(f'{fname}:{i}: {err}') from err
        conf.confdir = path.dirname(fname)
        return conf

    def __init__(self, *args: Any, **kwargs: Any):
        super().__init__(*args, **kwargs)
        self._login = None
        self._sections = []

    def __or__(self, other):
        """Merge two configurations."""
        conf = super().__or__(other)
        conf._sections = self._sections + other._sections
        return conf

    def __ior__(self, other):
        """Merge two configurations in-place."""
        super().__ior__(other)
        self._sections += other._sections
        return self

    def __ror__(self, other):
        """Merge two configurations in reverse."""
        conf = super().__ror__(other)
        conf._sections = other._sections + self._sections
        return conf

    def __repr__(self):
        """Get a string representation of the configuration."""
        variables = ', '.join((f'{k}={v!r}' for k, v in self.items()))
        return f'{self.__class__.__name__}({variables})'

    def getsection(self, host: str, login: Optional[str] = None) -> 'Config':
        """Get the configuration for `login` on `host`."""
        conf = self.__class__() | self
        for sec in self._sections:
            # pylint: disable=protected-access
            if host in (sec._host, sec.alias) and sec._login in (None, login):
                conf |= sec
        return conf

    alias = AttrProxy('alias')
    """Alias for a host."""

    backup = EnumAttrProxy('backup', BackupPol.NONE, cls=BackupPol)
    """Backup policy."""

    cadir = ExpAttrProxy('cadir')
    """Custom CA directory."""

    cafile = ExpAttrProxy('cafile')
    """Custom CA file."""

    confdir = ExpAttrProxy('confdir', f'{XDGCONFIGDIR}/sieve')
    """Configuration directory."""

    confirm = FlagAttrProxy('confirm', ShellCmd.ALL, cls=ShellCmd)
    """Which shell commands have to be confirmed?"""

    cert = ExpAttrProxy('cert')
    """Client TLS certificate."""

    debugauth = BoolAttrProxy('debugauth', False)
    """Log the authentication exchange?"""

    getkeypass = CmdAttrProxy('getkeypass')
    """Command that prints the passphrase for the TLS key."""

    getpass = CmdAttrProxy('getpass')
    """Command that prints a password."""

    host = HostAttrProxy('host', 'localhost')
    """Host to connect to by default."""

    key = ExpAttrProxy('key')
    """Client TLS key."""

    login = AttrProxy('login')
    """Login name to use by default."""

    ocsp = BoolAttrProxy('ocsp', True)
    """Check whether server certificate was revoked?"""

    port = IntAttrProxy('port', 4190, min=0, max=65535)
    """Port to connect to by default."""

    saslmechs = AuthAttrProxy('saslmechs', BasePassAuth.getmechs())
    """How to authenticate."""

    saslprep = FlagAttrProxy('saslprep', SASLPrep.ALL, cls=SASLPrep)
    """Which credentials to prepare."""

    timeout = FloatAttrProxy('timeout', socket.getdefaulttimeout(), min=0)
    """Network timeout."""

    tls = BoolAttrProxy('tls', True)
    """Use TLS?"""

    verbosity = EnumAttrProxy('verbosity', LogLevel.INFO, cls=LogLevel)
    """Logging level."""

    x509strict = BoolAttrProxy('x509strict', True)
    """Be strict when verifying TLS certificates?"""

    _host: Optional[str] = None
    """Section host (if this is a section)."""

    _login: Optional[str] = None
    """Section login (if this is a section)."""

    _matchcmnt: Callable[..., Optional[re.Match]] = re.compile(r'\s*#').match
    """Check whether a line is a comment."""

    _sections: list['Config'] = []
    """Configuration sections (if this is the global configuartion)."""


#
# Password stores
#

class BasePasswordStore(ABC):
    """Base class for retrieving credentials from password stores."""

    @abstractmethod
    def getlogin(self, host: str) -> Optional[str]:
        """Get a login name for `host`."""

    @abstractmethod
    def getpass(self, host: str, login: str) -> Optional[str]:
        """Get the password for `login` on `host`."""


class ConfigPasswordStore(BasePasswordStore):
    """Read credentials from a SieveManager configuration file."""

    def __init__(self, conf: 'Config'):
        """Initialise a :class:`ConfigPasswordStore` object."""
        self.conf = conf

    def getlogin(self, host: str) -> Optional[str]:
        conf = self.conf | self.conf.__class__(host=host)
        return conf.login

    def getpass(self, host: str, login: str) -> Optional[str]:
        conf = self.conf | self.conf.__class__(host=host, login=login)
        if getpass := conf.getpass:
            try:
                return output_read(*getpass)
            except subprocess.CalledProcessError:
                pass
        return None


class NetRC(netrc.netrc, BasePasswordStore):
    """Read credentials from :file:`.netrc`.

    .. seealso::
        *GNU network utilities manual* (chap. `11.7 <netrc_>`_)
            Discussion of the :file:`.netrc` file format.
    """

    def getlogin(self, host: str) -> Optional[str]:
        try:
            return self.hosts[host][0]
        except KeyError:
            return None

    def getpass(self, host: str, login: str) -> Optional[str]:
        try:
            return self.hosts[host][2]
        except KeyError:
            return None


class PasswordStoreList(list, BasePasswordStore):
    """Read credentials from multiple password stores."""

    def getlogin(self, host: str):
        for store in self:
            if login := store.getlogin(host):
                return login
        return None

    def getpass(self, host: str, login: str):
        for store in self:
            if passwd := store.getpass(host, login):
                return passwd
        return None


def output_read(*command: str, encoding: str = ENCODING):
    """Read and decode the output of `command`."""
    logging.debug('exec: %s', ' '.join(command))
    cp = subprocess.run(command, capture_output=True, check=True)  # nosec B603
    return cp.stdout.rstrip().decode(encoding)


#
# Logging
#

class LogIOWrapper():
    """Logger for file-like objects."""

    @classmethod
    def wrapfile(cls, *args, **kwargs):
        """Wrap a file in a :class:`LogIOWrapper` if logging is enabled.

        Takes the same arguments as :meth:`__init__`.

        Returns:
            The file or a :class:`LogIOWrapper` that wraps the file.
        """
        obj = cls(*args, **kwargs)
        if obj.logger.isEnabledFor(obj.level):
            return obj
        return obj.file

    def __init__(self, file: io.IOBase, encoding: str = 'utf8',
                 level: int = logging.DEBUG,
                 logger: logging.Logger = logging.getLogger(__name__),
                 formats: tuple[str, str] = ('S: %s', 'C: %s')):
        """Log I/O to `file`.

        Arguments:
            file: File-like object opened in binary mode.
            encoding: `file`'s encoding.
            level: Logging priority.
            logger: Logger.
            formats: Message formats; '%s' is replaced with I/O.
        """
        splitlines = re.compile(rb'\r?\n').split
        buffers = (bytearray(), bytearray())

        def cleanup():
            file.__del__()
            if self.log:
                for buf, fmt in zip(buffers, formats):
                    if buf:
                        log(buf, fmt)

        def extv(buf: bytearray, vec: Iterable[bytes]):
            for elem in vec:
                buf.extend(elem)

        def getwrapper(
            buf: bytearray, fmt: str, arg=None,
            ext: Callable[[bytearray, Iterable], None] = bytearray.extend
        ) -> Callable[[Callable[..., T]], Callable[..., T]]:
            def decorator(op: Callable[..., T]) -> Callable[..., T]:
                def wrapper(*args, **kwargs) -> T:
                    retval = op(*args, **kwargs)
                    if self.log:
                        ext(buf, retval if arg is None else args[arg])
                        ptr: Union[bytes, bytearray] = buf
                        while True:
                            try:
                                line, ptr = splitlines(ptr, maxsplit=1)
                            except ValueError:
                                break
                            log(line, fmt)
                        buf[:] = ptr
                    return retval
                return wrapper
            return decorator

        def log(line: bytes, fmt: str):
            string = ctrl_escape(line.rstrip(b'\r\n').decode(encoding))
            logger.log(level, fmt, string)

        wrapread = getwrapper(buffers[0], formats[0])
        wrapreadv = getwrapper(buffers[0], formats[0], ext=extv)
        wrapwrite = getwrapper(buffers[1], formats[1], 0)
        wrapwritev = getwrapper(buffers[1], formats[1], 0, extv)
        self.__del__ = cleanup
        self.read = wrapread(file.read)
        self.readline = wrapread(file.readline)
        self.readlines = wrapreadv(file.readlines)
        self.write = wrapwrite(file.write)
        self.writelines = wrapwritev(file.writelines)
        self.file = file
        self.level = level
        self.logger = logger

    def __getattr__(self, name):
        """Pass message to :attr:`file`."""
        return getattr(self.file, name)

    file: io.IOBase
    """Underlying file-like object."""

    level: int
    """Logging level."""

    log: bool = True
    """Log I/O?"""

    logger: logging.Logger
    """Logger."""

    read: Callable[..., bytes]
    """Read from :attr:`file`."""

    readinto: Callable[..., int]
    """Read from :attr:`file` into a buffer."""

    readline: Callable[..., bytes]
    """Read a line from :attr:`file`."""

    readlines: Callable[..., list[bytes]]
    """Read all lines from :attr:`file`."""

    write: Callable[[bytes], Optional[int]]
    """Write to :attr:`file`."""

    writelines: Callable[[Iterable[bytes]], Optional[int]]
    """Write lines to :attr:`file`."""


#
# Errors
#

# Error types
class Error(Exception):
    """Base class for errors."""


class CapabilityError(Error):
    """Base class for capability errors."""


class ConfigError(Error):
    """Base class for configuration errors."""


class DataError(Error):
    """Base class for data errors."""


class OperationError(Error):
    """Base class for operation errors."""


class ProtocolError(Error):
    """Base class for protocol errors.

    .. warning::
        Continuing after a :exc:`ProtocolError` is unsafe.
    """


class SecurityError(Error):
    """Base class for security errors.

    .. warning::
        Continuing after a :exc:`SecurityError` is unsafe.
    """


class UsageError(Error):
    """Base class for usage errors."""


# Client errors
class ClientError(Error):
    """Base class for client errors."""


class ClientConnectionError(ClientError, ConnectionError):
    """Connection error."""


class ClientOperationError(ClientError, OperationError):
    """Client-side operation error."""


# DNS errors
class DNSError(Error):
    """Base class for DNS errors."""


class DNSOperationError(DNSError, OperationError):
    """DNS operation error."""


# HTTP errors
class HTTPError(Error):
    """Base class for HTTP errors."""


class HTTPOperationError(HTTPError, OperationError):
    """HTTP operation error."""


class HTTPUsageError(HTTPError, ProtocolError):
    """HTTP usage error."""


# OCSP errors
class OCSPError(Error):
    """Base class for OCSP_ errors."""


class OCSPDataError(OCSPError, DataError):
    """OCSP_ data error."""


class OCSPOperationError(OCSPError, OperationError):
    """OCSP_ operation error."""


# SASL errors
class SASLError(Error):
    """Base class for SASL errors."""


class SASLCapabilityError(Error):
    """SASL capability error."""


class SASLProtocolError(SASLError, ProtocolError):
    """Server violated the SASL protocol."""


class SASLSecurityError(SASLError, SecurityError):
    """SASL security error."""


# Shell errors
class ShellError(Error):
    """Base class for shell errors."""


class ShellConfigError(ClientError, ConfigError):
    """Shell configuration error."""


class ShellUsageError(ShellError, UsageError):
    """Shell usage error."""


# ManageSieve errors
class SieveError(Error):
    """Base class for ManageSieve errors."""


class SieveCapabilityError(SieveError, CapabilityError):
    """Capability not supported by the server."""


class SieveConnectionError(Response, SieveError, ConnectionError):
    """Server said "BYE"."""

    # pylint: disable=redefined-outer-name
    def __init__(self, response: str = 'bye',
                 code: tuple[ACAPWord, ...] = (),
                 message: Optional[str] = None):
        super().__init__(response=response, code=code, message=message)


class SieveOperationError(Response, SieveError):
    """Server said "NO"."""

    # pylint: disable=redefined-outer-name
    def __init__(self, response: str = 'no',
                 code: tuple[ACAPWord, ...] = (),
                 message: Optional[str] = None):
        super().__init__(response=response, code=code, message=message)


class SieveProtocolError(SieveError, ProtocolError):
    """Server violated the ManageSieve protocol error."""


# TLS errors
class TLSError(Error):
    """Base class for TLS errors."""


class TLSCapabilityError(TLSError, CapabilityError):
    """TLS capability error."""


class TLSSecurityError(TLSError, OperationError):
    """TLS security error."""


#
# Exceptions
#

@dataclass(frozen=True)
class SignalCaught(Exception):
    """Signal was caught."""

    @classmethod
    def catch(cls, signo: int, frame: Optional[types.FrameType]):
        """Raise a :exc:`SignalCaught`."""
        raise cls(signo, frame)

    def __str__(self):
        desc = signal.strsignal(self.signo)
        return desc.split(':')[0] if desc else f'caught signal {self.signo}'

    signo: int
    """Signal number."""

    frame: Optional[types.FrameType] = None
    """Stack frame."""


#
# Main
#

# getpass.getpass cannot write to /dev/tty on Linux.
def passwd_prompt(prompt: str = 'Password: ',
                  encoding: str = ENCODING) -> str:
    """Prompt for a password."""
    fd = os.open('/dev/tty', os.O_RDWR)
    tty = os.fdopen(fd, encoding=encoding)
    old = termios.tcgetattr(fd)
    try:
        new = termios.tcgetattr(fd)
        new[3] &= ~termios.ECHO
        termios.tcsetattr(fd, termios.TCSADRAIN, new)
        os.write(fd, prompt.encode(encoding))
        passwd = tty.readline().rstrip('\n')
        os.write(fd, b'\n')
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old)
        tty.close()
    return passwd


def config_find(basename: str) -> Optional[str]:
    """Find a configuration file.

    Looks in:
        * :samp:`$XDG_CONFIG_HOME/{basename}/config`
        * :samp:`$HOME/.{basename}/config`
        * :samp:`$HOME/.{basename}.cf`
        * :samp:`/etc/{basename}/config`
        * :samp:`/etc/{basename}.cf`
    """
    fnames = (f'{XDGCONFIGDIR}/{basename}/config',
              f'{HOMEDIR}/.{basename}/config',
              f'{HOMEDIR}/.{basename}.cf',
              f'/etc/{basename}/config',
              f'/etc/{basename}.cf')
    for fname in fnames:
        if path.exists(fname):
            return fname
    return None


# pylint: disable=too-many-branches, too-many-locals, too-many-statements
def sievemgr() -> int:  # noqa: C901
    """sievemgr - manage remote Sieve scripts

    Usage:  sievemgr [server] [command] [argument ...]
            sievemgr -e expression [...] [server]
            sievemgr -s file [server]

    Options:
        -C             Do not overwrite existing files.
        -c file        Read configuration from file.
        -d             Enable debugging mode.
        -e expression  Execute expression on the server.
        -f             Overwrite and remove files without confirmation.
        -i             Confirm removing or overwriting files.
        -o key=value   Set configuration key to value.
        -q             Be quieter.
        -s file        Execute expressions read from file on the server.
        -v             Be more verbose.

        -e, -q, and -v can be given multiple times.
        See sievemgr(1) for the complete list.

    Home page: https://odkr.codeberg.page/sievemgr
    """
    progname = path.basename(sys.argv[0])
    logging.basicConfig(format=f'{progname}: %(message)s')

    for signo in (SIGHUP, SIGINT, SIGTERM):
        signal.signal(signo, SignalCaught.catch)

    # Options
    clobber = True
    optconf = Config()
    exprs: list[str] = []
    logdelta = 0
    netrcfile = os.getenv('NETRC')
    configfile = config_find('sieve')
    script: Optional[TextIO] = None
    opts, args = getopt(sys.argv[1:], 'CN:Vc:de:fhio:qs:v',
                        ['help', 'version'])
    for opt, arg in opts:
        try:
            if opt in ('-h', '--help'):
                assert sievemgr.__doc__     # nosec B101
                for line in sievemgr.__doc__.splitlines()[:-1]:
                    print(re.sub(r'^ {4}', '', line))
                sys.exit()
            elif opt in ('-V', '--version'):
                SieveShell.do_about()
                sys.exit()
            elif opt == '-C':
                clobber = False
            elif opt == '-N':
                netrcfile = arg
            elif opt == '-c':
                configfile = arg
            elif opt == '-d':
                # pylint: disable=global-statement
                global DEBUG
                DEBUG = True
                optconf.verbosity = LogLevel.DEBUG
            elif opt == '-e':
                exprs.append(arg)
            elif opt == '-f':
                optconf.confirm = ShellCmd.NONE
            elif opt == '-i':
                optconf.confirm = ShellCmd.ALL
            elif opt == '-o':
                try:
                    key, value = arg.split('=', maxsplit=1)
                except ValueError as err:
                    key = arg
                    if isinstance(optconf[key], bool):
                        value = True    # type: ignore[assignment]
                    else:
                        raise ValueError(f'{key}: no value') from err
                if value == '':
                    raise ValueError(f'{key}: empty')
                optconf[key] = value
            elif opt == '-q':
                logdelta -= 1
            elif opt == '-s':
                # pylint: disable=consider-using-with
                script = open(arg, 'r', encoding=ENCODING)
            elif opt == '-v':
                logdelta += 1
        except KeyError as err:
            raise ShellUsageError(f'{opt}: {key}: no such key') from err
        except (Error, ValueError) as err:
            raise ShellUsageError(f'{opt}: {err}') from err

    # Preliminary configuration
    conf = (Config.fromfile(configfile) | optconf) if configfile else optconf

    # Arguments
    url = URL.fromstr(args.pop(0)) if args else URL(hostname=conf.host)
    command = args.pop(0) if args else ''

    if script:
        if command:
            raise ShellUsageError('-s cannot be used when a command is given')
        if exprs:
            raise ShellUsageError('-e and -s cannot be combined')

    # Final configuration
    conf = conf.getsection(url.hostname, url.username) | optconf

    # Logging level
    levels = list(LogLevel)
    logindex = levels.index(conf.verbosity) - logdelta
    level = levels[min(max(logindex, 0), len(levels) - 1)]
    logging.getLogger().setLevel(level)
    logauth = level <= LogLevel.AUTH

    # Infos
    for line in ABOUT.strip().splitlines():
        logging.info('%s', line)

    # Password stores
    credentials = PasswordStoreList([ConfigPasswordStore(conf)])
    try:
        if netrcfile:
            credentials.append(NetRC(netrcfile))
        else:
            try:
                credentials.append(NetRC())
            except FileNotFoundError:
                pass
    except netrc.NetrcParseError as err:
        if sys.version_info < (3, 10):
            logging.error(err)
        else:
            raise

    # TLS
    if conf.isset('cadir') or conf.isset('cafile'):
        SieveManager.sslcontext.load_verify_locations(conf.cafile, conf.cadir)
    if cert := conf.cert:
        def getpassphrase():
            if getkeypass := conf.getkeypass:
                return output_read(*getkeypass)
            return passwd_prompt('Certificate passphrase: ')
        SieveManager.sslcontext.load_cert_chain(cert, conf.key, getpassphrase)
    if conf.x509strict:
        SieveManager.sslcontext.verify_flags |= ssl.VERIFY_X509_STRICT

    # Connection and authentication information
    host = conf.host if conf.alias == url.hostname else url.hostname
    port = url.port if url.port else conf.port
    login = url.username or credentials.getlogin(host) or getpass.getuser()
    owner = url.owner if url.owner else ''
    sasl = conf.saslmechs

    # Connect and start the shell
    with SieveManager(host, '', port=port, timeout=conf.timeout,
                      tls=conf.tls, ocsp=conf.ocsp,
                      backup=conf.backup) as mgr:
        if issubclass(sasl[0], BasePassAuth):
            password = (url.password
                        or credentials.getpass(host, login)
                        or passwd_prompt())
            mgr.authenticate(login, password, owner=owner, sasl=sasl,
                             logauth=logauth, prepare=conf.saslprep)
        elif issubclass(sasl[0], ExternalAuth):
            mgr.authenticate(login, owner=owner, sasl=sasl,
                             logauth=logauth, prepare=conf.saslprep)
        else:
            raise NotImplementedError('unsupported authentication type')
        shell = SieveShell(mgr, clobber=clobber, confirm=conf.confirm)
        if exprs:
            for expr in exprs:
                shell.executeline(expr)
        elif script:
            shell.runscript(script)
        elif command:
            shell.execute(command, *args)
        else:
            shell.enter()
    return shell.retval


def main():
    """Call :func:`sievemgr` but catch exceptions."""
    try:
        retval = sievemgr()
    except SignalCaught as exc:
        logging.error(exc)
        signal.signal(SIGTERM, SIG_IGN)
        os.killpg(os.getpgrp(), SIGTERM)
        signal.signal(exc.signo, SIG_DFL)
        signal.raise_signal(exc.signo)
    except (GetoptError, UsageError) as err:
        logging.error(err)
        sys.exit(2)
    # pylint: disable=broad-exception-caught
    except Exception as err:
        if DEBUG:
            raise
        if isinstance(err, (socket.herror, socket.gaierror, ConnectionError)):
            logging.error('%s', err.args[1])
        elif isinstance(err, (FileExistsError, FileNotFoundError)):
            logging.error('%s: %s', err.filename, os.strerror(err.errno))
        elif isinstance(err, (MemoryError, ssl.SSLError, Error)):
            logging.error('%s', err)
        # Some modules use OSError wrong.
        elif isinstance(err, OSError) and err.errno:
            logging.error('%s', os.strerror(err.errno))
        elif isinstance(err, subprocess.CalledProcessError):
            logging.error('%s: exited with status %d',
                          err.cmd[0], err.returncode)
        else:
            logging.exception(err)
        sys.exit(1)
    sys.exit(retval)


#
# Logging
#

logging.getLogger(__name__).addHandler(logging.NullHandler())


#
# Boilerplate
#

if __name__ == '__main__':
    main()
